PDA

View Full Version : for loop issue



thesprucegoose
03-31-2009, 05:10 PM
All,

I'm having a bit of issue with a for loop.




for(i = 0; i < captionID.length; i++) {

document.getElementById("captainContent").innerHTML = captionID[i];

}


This returns only the last number in the caption[120] rather than printing
captionID[0]
captionID[1]
captionID[2]
captionID[3]
etc

Any ideas?

Thanks,
--thesprucegoose

jscheuer1
03-31-2009, 05:25 PM
This is not the ideal way, but the easiest to add to what you currently have:


document.getElementById("captainContent").innerHTML += captionID[i] + '<br>';

I'll present a DOM solution (the preferred method) in a bit (when I have the time, if not sooner).

jscheuer1
03-31-2009, 05:36 PM
Better:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="captionContent"></div>
<script type="text/javascript">
(function(){
var captionID = [], i = 0, b = document.createElement('br'),
c = document.getElementById('captionContent'), l;
captionID[0] = 'The';
captionID[1] = 'Quick';
captionID[2] = 'Brown';
captionID[3] = 'Fox';
l = captionID.length;
for(i; i < l; ++i) {
c.appendChild(document.createTextNode(captionID[i]));
c.appendChild(b.cloneNode(false));
}
})();
</script>
</body>
</html>