Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  168] [ 6]  / answers: 1 / hits: 37753  / 12 Years ago, sun, october 21, 2012, 12:00:00

this is my coding. in this coding i want to print table in innerHTML in

paragraph tag.This is working but the problem is when i click submit button this result show only last value like this 10*10=100 but i want to run full loop which i define in code from 1 till 10. Please solve this issue.



<html>
<head>
<title>Table Program</title>
</head>
<body>
<input type=text id=table placeholder=Enter table/>
<input type=button value=Submit onClick=table()/>
<p id=demo></p>
<!----------------------------------------------------------------------------------------------->
<script type=text/javascript>
function table()
{
var value = document.getElementById(table).value;
var demop = document.getElementById(demo);
var a;
for(a=1; a <= 10;++a)
{
demop.innerHTML=(value+*+ a +=+ value*a);
}
}
</script>
</body>
</html>

More From » innerhtml

 Answers
207

Your for loop is overwriting the innerHTML of demop each time it is executing.
So when your for loop reaches last iteration a will be 10 and hence you only get the last value 10*10=100



So you should append the result every time you iterate your for loop
just make it like this



demop.innerHTML += (value + * + a + = + (value*a) + <br />);



So you will get your output on seperate lines.


[#82450] Friday, October 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
albert

Total Points: 652
Total Questions: 105
Total Answers: 108

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;