loops - Javascript: Print Y, X times -


I think I'm probably making a fundamental mistake here, I hope someone can help me.

  Function countdown () {x = 5; For (i = x; i> I;) {document.getElementById ('display'). InnerHTML = (i + ""); }}  

This is a very small, simple replica of the problem.

I have a long task that is a variable in that function, in this case: X.

I want to insert something into an element (in this case: #desplay) many times of x

I thought the best way to do this would be with loop, it would be counted below x 1 and every time I want to insert the string.

But when it moves, it only gives 1. (I hope in this case "5 4 3 2 1" to return).

Please someone can tell me why this does not work? I am reaching my mind for hours for hours.

Because you overwriting the contents of the element in each walk, innerHTML By specifying a new value for You must add instead:

  document.getElementById ('display'). InnerHTML + = (i + ""); // ^  

which is similar to

  document.getElementById ('display'). InnerHTML = document.getElementById ('display'). InnerHTML + (I + "");  

And of course it would be great if you created the string in the loop and set the content after the loop:

  Var content = ''; For (i = x; i> I;) {content + = (i + ""); } Document.getElementById ('Display'). InnerHTML = content;  

In this way you avoid looking at the element in the document on every repetition.

P>


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -