Skip to content Skip to sidebar Skip to footer

Innerhtml With For Loop In Javascript

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 valu

Solution 1:

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.

Solution 2:

When you call demop.innerHTML = something you overwrite it each time. You should either:

  • Put the entire string in a temporary result variable and then give it to innerHTML
  • Concatenate the different results by doing

    demop.innerHTML = demop.innerHTML + (value+"*"+ a +"="+ value*a);

Post a Comment for "Innerhtml With For Loop In Javascript"