GoDxNero Posted August 8, 2009 Share Posted August 8, 2009 <html><body><script type="text/javascript">for (i = 1; i <= 3; i++){document.getElementById('rank').innerHTML=+i;}</script><table border="1"><tr><td id="rank"></td><td>name</td></tr><tr><td id="rank"></td><td>name</td></tr><tr><td id="rank"></td><td>name</td></tr></table></body></html> Link to comment Share on other sites More sharing options...
chibineku Posted August 8, 2009 Share Posted August 8, 2009 The operator you want is += However, since there isn't a number already assigned as the innerHTML value of the element you want, you need to assign it first. Do you want to increment the value by i each time, or just set the value to i? Link to comment Share on other sites More sharing options...
Synook Posted August 8, 2009 Share Posted August 8, 2009 Edit: chibineku said it first. By writing var = +i, you are just saying "assign positive i to var" (as opposed to negative i). Link to comment Share on other sites More sharing options...
dsonesuk Posted August 8, 2009 Share Posted August 8, 2009 (1) multiple id of same name, not allowed.(2) javascript will run when reached and because the table has not been created yet, it will not be able to find the id ref anyway, and give an undefined error.only way this will work is if you do this:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/function insertthis(){for (i=1; i<=3;i++){document.getElementById("rank"+i).innerHTML=i;}}window.onload=insertthis;/*--*//*]]>*/</script> </head><body><table border="1"><tr><td id="rank1"></td><td>name</td></tr><tr><td id="rank2"></td><td>name</td></tr><tr><td id="rank3"></td><td>name</td></tr></table></body></html> Link to comment Share on other sites More sharing options...
dsonesuk Posted August 8, 2009 Share Posted August 8, 2009 the other option is: (for two cell per row table)<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/function insertthis(){var x = document.getElementById("mytable");var y = x.getElementsByTagName("td");var l = y.length;j=1;for (i=0; i<l;i+=2){y.innerHTML=j;j++;}}window.onload=insertthis;/*--*//*]]>*/</script> </head><body><table border="1" id="mytable"><tr><td></td><td>name</td></tr><tr><td></td><td>name</td></tr><tr><td></td><td>name</td></tr></table></body></html> Link to comment Share on other sites More sharing options...
GoDxNero Posted August 8, 2009 Author Share Posted August 8, 2009 Thanks for your help, this is what I got now. And it seems to workJavascripts function initRanks(){var ranks = document.getElementsByClassName("rank");for(var i = 0; i < ranks.length; i++){ranks[i].innerHTML = i+1;}} Body <table><tr><td class="rank"></td><td></td></tr><tr><td class="rank"></td><td></td></tr><tr><td class="rank"></td><td></td></tr></table> Link to comment Share on other sites More sharing options...
dsonesuk Posted August 8, 2009 Share Posted August 8, 2009 as it is now, yes it will work for browsers other than IE, where it will fail with message "object doesn't support this property or method" Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.