javascript - Why does my for loop come once? -
i've got code loop in function..
look bit code please:
<!doctype html> <html> <head> <title>test</title> </head> <body> kies een tafel: <select id="tafels"> <option value="1">tafel 1</option> <option value="2">tafel 2</option> <option value="3">tafel 3</option> <option value="4">tafel 4</option> <option value="5">tafel 5</option> <option value="6">tafel 6</option> <option value="7">tafel 7</option> <option value="8">tafel 8</option> <option value="9">tafel 9</option> <option value="10">tafel 10</option> </select> <input type="submit" id="submit" value="bereken" onclick="tafel23();"> <div id="asd"> </div> <script> function tafel23(){ var value = document.getelementbyid('tafels').value; var value1 = parseint(value); var teller = 0; for(teller = 1; teller <= 10; teller++){ document.getelementbyid('asd').innerhtml=(value1 + " x " + teller + " = " + teller * value1 + "<br/>"); } } </script> </body> </html>
my loop come onces when click submit button... when innerhtml+= works, whole loop appearing, when press submit button again , again, loop stacking.. got problem is, when press submit button loop appear, not again , again..
thanks in advance guys!<3
try clearing div before execute loop:
function tafel23(){ var value = document.getelementbyid('tafels').value; var value1 = parseint(value); //you might check value1 nan here document.getelementbyid('asd').innerhtml=""; //clear out for(var teller = 1; teller <= 10; teller++){ document.getelementbyid('asd').innerhtml += value1 + " x " + teller + " = " + teller * value1 + "<br/>"; } }
Comments
Post a Comment