|
I have a code I am working on (updating) that lets admins style their categorys individually. I am using an array to choose the category to edit and assign that category an id which will then be styled through CSS. Here's what I have, (without the style sheet.) <script type= "text/javascript"> //Individual Category Styles var catName= ["General","Tester","New Category"] var newID= ["general","tester","newcat"]
var td= document.getElementsByTagName("td"),i; for(i=0;i<td.length;i++){ if(td[i].className== "catbg" && td[i].innerHTML.match(catName[0])) {td[i].id=newID[0]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[1])) {td[i].id=newID[1]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[2])) {td[i].id=newID[2]} }
</script> This works fine .... until the if statement doesn't find the category name. Let me elaborate: The arrays above hold the values of three category names and their chosen id's. The script below the arrays are associated with the arrays. The code works properly because there are indeed 3 categorys on my test site. But if I add more if statements the code doesn't work. I want to give this code the option to edit up 10 categorys and have the script hold 10 preset conditions so that the user only needs to edit the arrays in order to edit their individual categorys. Like this: <script type= "text/javascript"> //Individual Category Styles var catName= ["General","Tester","New Category"] var newID= ["general","tester","newcat"]
var td= document.getElementsByTagName("td"),i; for(i=0;i<td.length;i++){ if(td[i].className== "catbg" && td[i].innerHTML.match(catName[0])) {td[i].id=newID[0]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[1])) {td[i].id=newID[1]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[2])) {td[i].id=newID[2]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[3])) {td[i].id=newID[3]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[4])) {td[i].id=newID[4]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[5])) {td[i].id=newID[5]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[6])) {td[i].id=newID[6]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[7])) {td[i].id=newID[7]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[8])) {td[i].id=newID[8]} if(td[i].className== "catbg" && td[i].innerHTML.match(catName[9])) {td[i].id=newID[9]} } </script> But when I add all of them if statements, (past the 3rd one) the code stops working because the last 7 if statements doesn't find them instances of categorys I suppose. ?? Any help?
Last Edit: Aug 20, 2012 21:50:15 GMT by game
|
|
|
|
var catName= ["General","Tester","New Category", "4", "5", "6", "7", "8", "9", "10"] var newID= ["general","tester","newcat", "4", "5", "6", "7", "8", "9", "10"]
your code is looking for items in your array that don't exists. you can't have "catName[4]" in your code if it only goes to 2, that's why you're getting errors.
|
|
|
|
var catName= ["General","Tester","New Category", "4", "5", "6", "7", "8", "9", "10"] var newID= ["general","tester","newcat", "4", "5", "6", "7", "8", "9", "10"]
your code is looking for items in your array that don't exists. you can't have "catName[4]" in your code if it only goes to 2, that's why you're getting errors. Yea, i figured out why. I used a different method which worked very much better. <script type= "text/javascript"> //Individual Category Styles //PLacement: Footers //Coded By Game var catEdit=[//Enter [''Category Name'',''Category ID"] below. ["Category Name","Category ID"], ["Category Name","Category ID"] ]; //Editing Not Required Below var td= document.getElementsByTagName("td"),i,x;for(i=0; i<catEdit.length;i++){for(x=0;x<td.length;x++){if(td[x].className=="catbg" && td[x].innerHTML.match(catEdit[i][0])){ td[x].id=catEdit[i][1];}}} </script>
Last Edit: Aug 21, 2012 3:46:03 GMT by game
|
|
|
|
try in_array. Your current code loops all the table cell tags on your page for every item in your array, which isn't very optimal.
|
|
|
|
Hmm, I see your point and yes, I'll have a closer look at that. Thank You
|
|
|