| SitePoint Sponsor |

Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
Having lame problems with your code? Let us help by using a jsFiddle
length attribute will give you the length of the array, just type arrayname.length
![]()


What would you say should be used?
this
Code JavaScript:function get_active_link(myul, myclass){ var menu_unidades_yolo = document.getElementById(myul); menu_unidades_yolo = menu_unidades_yolo.getElementsByTagName('a'); var number=menu_unidades_yolo.length; var current_url = document.location; while (number >= 0){ if (menu_unidades_yolo[number].href == current_url){ menu_unidades_yolo[number].className = myclass; } number--; } }
or this
Code JavaScript:function get_active_link(myul, myclass){ var menu_unidades_yolo = document.getElementById(myul); menu_unidades_yolo = menu_unidades_yolo.getElementsByTagName('a'); var current_url = document.location; for (var i in menu_unidades_yolo){ if (menu_unidades_yolo[i].href == current_url){ menu_unidades_yolo[i].className = myclass; } } }


The while loop is better than the for...in one, but for the easiest to understand code you should use a normal for loop to iterate through the array items.
Code javascript:function get_active_link(myul, myclass){ var menu_unidades_yolo = document.getElementById(myul).getElementsByTagName('a'), linksLen = menu_unidades_yolo.length, current_url = document.location, i, link; for (i = 0; i < linksLen; i++) { link = menu_unidades_yolo[number]; if (link.href == current_url) { link.className = myclass; } } }
Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript
Bookmarks