Sololearn: Learn to Code
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2
Ok so you want to know the difference between getElementById or getElementByClassName and querySelector: querySelector uses CSS selectors to take an element from the DOM getElementById can only select one element from the DOM. getElementByClassName and querySelector can select various elements. getElementByClassName returns a lista with all elements that match with the given class (it happens the same if querySelector selects classes) Hope this is helpful
9th May 2023, 3:47 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 2
querySelector is used for taking elements from the DOM (as you probably know) You can use it for selecting classes var obj = document.querySelector(“.title”) If there are some objects with the same class: var obj2 = document.querySelector(“.title”)[1] -> Second element with class title You can also do the same with ids: var obj = document.querySelector(“#title”) And you can use querySelectorAll for selecting various elements: var obj = document.querySelectorAll(“.title”) -> All elements with class title You can use querySelector for taking, for example, links: let link = document.querySelector('a[href*="https://google.com"]');
9th May 2023, 3:27 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 2
What do you mean? Isnt my answer working?
9th May 2023, 3:40 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 1
9th May 2023, 4:00 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar