+ 1

Why to use 'querySelector' instead of 'getElementById' or 'getElementByClass' in JavaScript?

23rd May 2021, 10:54 AM
Mayank Gupta
Mayank Gupta - avatar
5 Respostas
+ 3
Mayank Gupta The querySelector method lets you retrieve an element using a CSS selector query. The getElementById method retrieves an element by its DOM ID. Both methods have wide browser support. You should opt to use the querySelector method if you need to select elements using more complex rules that are easily represented using a CSS selector. If you want to select an element by its ID, using getElementById is a good choice. Now you have the knowledge you need to know when to use the querySelector and getElementById methods like a pro!
23rd May 2021, 11:08 AM
Matias
Matias - avatar
+ 1
Because you can use css selector in querySelector() . To select a div having class name one . you can use directly document.querySelector("div.one");
23rd May 2021, 10:59 AM
TOLUENE
TOLUENE - avatar
+ 1
Matiyas Thanks
23rd May 2021, 11:09 AM
Mayank Gupta
Mayank Gupta - avatar
0
querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
23rd May 2021, 10:58 AM
Matias
Matias - avatar
0
Matiyas I wanted to know that if we can use 'getElement' function instead of 'querySelector'.
23rd May 2021, 11:05 AM
Mayank Gupta
Mayank Gupta - avatar