+ 1

Confused here , topic from DOM(2nd topic in Selecting elements) in Js.so someone please clarify me

The lesson shows[2nd topic from selecting elements in DOM- JavaScript] :- The getElementsByClassName() method returns a collection of all elements in the document with the specified class name. For example, if our HTML page contained three elements with class="demo", the following code would return all those elements as an array: var arr=document.getElementByClassName("demo"); //accessing the second element. arr[1].innerHTML="Hi!"; From the above description and example, how it is possible to code will return all those elements in the array. According to me it Actually returns only one element( I.e, second element arr[1]) from the array. but description shows :- /* For example, if our HTML page contained three elements with class="demo", the following code would return all those elements as an array: */ Comment in the code shows:-, //accessing the second element. So confused here,someone please clarify me.

5th Jun 2020, 7:34 AM
Ajitha G R
Ajitha G R - avatar
2 Réponses
+ 3
you can add a line console.log (arr) it should print "HTMLCollection" so arr is storing a HTML Collection https://code.sololearn.com/W49EnvRjgCO1/?ref=app when you use arr[1], you are accessing the second element in this HTML Collection.
5th Jun 2020, 7:37 AM
Gordon
Gordon - avatar
0
Thank you Gordon
5th Jun 2020, 12:02 PM
Ajitha G R
Ajitha G R - avatar