I am using a for-in loop to iterate over an array of two elements, but it is like its looping twice.
I have an example here: http://jsbin.com/etoyac/8/
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s because the result returned from
getElementsByTagNameisn’t a simple array — it’s a NodeList and it’s “live”. Updates you make to the DOM are reflected in the list immediately.You can use the “slice” method on the Array prototype to turn the NodeList into an array:
Also you should iterate through the array (or the NodeList for that matter) with a numeric index: