+ 1
Why to use 'querySelector' instead of 'getElementById' or 'getElementByClass' in JavaScript?
5 Answers
+ 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!
+ 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");
+ 1
Matiyas Thanks
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.
0
Matiyas
I wanted to know that if we can use 'getElement' function instead of 'querySelector'.