pengwang
pengwang

Reputation: 19966

how to use js to do a loop

i want to use js this function: i have a string[] s={"11111","2222","33333",...} how to achieve the html body

<h2>total s.length </h2>
<p>s[0]</p>
<p>s[1]</p>
<p>s[2]</p>

because s.length and content is uncertain,so i cannot one by one print,i want to use javascript,can you tell me how,i know little about js knowledge.thank you

Upvotes: 0

Views: 113

Answers (1)

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

var arr = ["11111","2222","33333"];
var output = "<h2>total " + arr.length + "</h2>" + "<p>" + arr.join("</p><p>") + "</p>";
document.getElementById("OutputPanel").innerHTML = output;

Live demo: http://jsfiddle.net/TQSK4/

You better add the result into existing container in the document, like in the example, not write directly to the document.

Upvotes: 3

Related Questions