Use the querySelectorAll() to Return a NodeList That is why getElementsB圜lassName() will return an HTMLCollection, even if there is only one element with that class name.Īn element in the HTMLCollection can be accessed in the same way as elements in an array by putting the index of the element inside (square brackets). The reason for this is because an ID should only be used once on an element in an HTML document while a class can be put on as many elements as needed. You will notice that the JavaScript getElementsB圜lassName() method is plural unlike the singular getElementById(). getElementsB圜lassName ( 'fruit' ) console. To get an HTMLCollection of elements with a class use the getElementsB圜lassName() native JavaScript method. Getting an element by its ID is easy with JavaScript, but what about getting elements by class name? In this tutorial, we will learn how.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |