I am writing a code for proboards.com. The code is going to be used to give categorys individual styles.
In the array:
catEdit[0] is the category name and
catEdit[1] is the id that the user wishes to give that cateory so that they can apply styles to it using CSS.
In the script below I have first looped through the array and then looped through the categorys on the users forum, stated an argument, and based on the argument gave the category an id.
Something is not right. Can someone please tell me what I am doing wrong?
Thank you in advance.
<script type= "text/javascript">
//Individual Category Styles
var catEdit=[
["General", "general"],
["Tester", "tester"],
["New Category", "newcat"]
];
var td= document.getElementsByTagName("td"),i;
for(i=0; i<catEdit.length;i++){
for(i=0; i<td.length;i++){
if(td[i].className== "catbg" && td[i].innerHTML.match(catEdit[i][0])) {
td[i].id=catEdit[i][1]
}
}
}
</script>
-
Expected output? Actual Output? Perhaps you could be more specific about the problem.JohnFx– JohnFx2012年08月21日 01:01:59 +00:00Commented Aug 21, 2012 at 1:01
2 Answers 2
You should definitely not use i as the loop variable for both loops. Consider using a different variable for the inner loop.
3 Comments
td.length is less than catEdit.length there'll be an endless loop... If td.length is greater than catEdit.length the code'll crash trying to do catEdit[i][0].Both of your loops have the same index variable, i, which will not work well at all. Consider using i and j as the index variables for the loops:
for (var i = 0; i < catEdit.length; i++) {
for (var j = 0; j < td.length; j++) {
...