|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Using Loops
Ok, how do i explain this? Heres my set up: i have an input box, and a textArea box. I want to create a function that basically demonstrates the power of a loop. To do this, i wanted for a person to basically type a number in an input text field, and then have that number be the thing that is compared for the loops primary conditions, such as for(i=0; i < inputBox.value; i++). How do use a loop in a function that takes one of its comparisons from a text box? Heres what i have for a code:
<script type="text/javascript"> function myLooper() { inputBox = document.frmOne.inputBox.value; displayBox = document.frmOne.displayBox; var multiplier = inputBox.value; for (var i = 0; i < inputBox.value; i++){ answer = i * multiplier; displayBox.value += "I am a loop " + i + answer + "\n"; } } </script> Any ideas on how my goal could be achieved? |
|
#2
|
||||
|
||||
|
For some reason you attempt to get the inputbox value twice, but do it incorrectly the second time.... anyway, I've rewritten it and got it working. I haven't used a text area as it's easier to just print to screen. Feel free to rem the alert line.
Code:
function myLooper() {
inputBox = document.form1.textfield.value;
displayBox = document.getElementById("output")
for (var i = 0; i < inputBox; i++){
answer = i * inputBox;
alert(answer)
displayBox.innerHTML += i + ". " + answer + "<br>";
}
}
</script>
</head>
<body>
<form name="form1" id="form1" method="post" action="">
<p>
<input type="text" name="textfield" />
</p>
<p> </p>
<div id="output"></div>
<input type="button" onclick="myLooper()">
</form>
</body>
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Using Loops |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|