Going back to the question, you asked how I was using length here it is
var menu = document.getElementById('someid');
menu_ = menu.getElementsByTagName('a');
alert(menu_.lenght);
var x = menu_.lenght;
alert(x);
Both alerts give me undefined however if I assign the lenght of the array myself I can loop through the elements without any problems, also what anarch posted works fine although it works I would like to learn what am I doing wrong when using the lenght property
WOW, I can´t beleive this, the spelling was my problem oh well, next time when something does not work as expected I will step back and look closely before I ask any more of this stupid questions.
No question is ever stupid, some of the SitePoint mentors and staff even ask questions because no matter what you know a challenge always lays waiting in the road ahead.
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
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.
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;
}
}
}