GoDxNero Posted August 8, 2009 Report 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 Report 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 Report Share Posted August 8, 2009 (edited) Edit: chibineku said it first. By writing var = +i, you are just saying "assign positive i to var" (as opposed to negative i). Edited August 8, 2009 by Synook Link to comment Share on other sites More sharing options...
dsonesuk Posted August 8, 2009 Report Share Posted August 8, 2009 (edited) (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> Edited August 8, 2009 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted August 8, 2009 Report Share Posted August 8, 2009 (edited) 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> Edited August 8, 2009 by dsonesuk Link to comment Share on other sites More sharing options...
GoDxNero Posted August 8, 2009 Author Report 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 Report 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
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now