Number of elements in array

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

The spelling is length, not lenght

When you assign it yourself to lenght you are instead creating a separate property value on that array.

lol… i already told you I understood all of that :lol:

now lets see your views on x64 assembly arrays before you start drilling people’s spelling


PUBLIC TestU
TestU PROC
   ADD    ECX, DWORD PTR [RSP+11H] 
   ADD    ECX, R9D                 
   ADD    ECX, R8D                 
   ADD    ECX, EDX                 
   MOVD   XMM0, ECX                
   CVTDQ2PD  XMM0, XMM0            
   MOVSD  XMM1, realVal            
   ADDSD  XMM1, MMWORD PTR [RSP+20H]
   DIVSD  XMM0, XMM1               
   RET                             
TestU ENDP
End

how r i implementing your beloved JS?

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.

Thank you

This is when people need to stop and take another think about things.

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.

length attribute will give you the length of the array, just type arrayname.length
:slight_smile:

What would you say should be used?

this


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;
        }
    }
}