+ 5

Can we use classes insted of id in JavaScript?

23rd Apr 2018, 2:37 PM
HET SHAH
HET SHAH - avatar
3 odpowiedzi
+ 6
Sure. If the class is only assigned to one element, you can easily get the element. If the class is assigned to multiple, you need to know the creation order so you can access the one you want. classes = document.getElementsByClassName("mine"); classes[0] is first classes[9] is tenth.
23rd Apr 2018, 2:47 PM
John Wells
John Wells - avatar
+ 4
yes classes can be used instead of an id.. Classes and Ids are like hooks. See link below for more explanation https://css-tricks.com/the-difference-between-id-and-class/
23rd Apr 2018, 2:44 PM
Awele Omeligwe
Awele Omeligwe - avatar
0
Possible. It will depend of the way you use it. Eg. The document.getElementByclass(). only if you it before as in html <div class="myclass">Here you done it</div>. The same way you called 'id'. Correct it if you want, I am yours.
20th Oct 2020, 5:30 AM
Fanlight 😎
Fanlight 😎 - avatar