+ 14

querySelector vs getElementByID

What is the difference between querySelector and getElementById

19th Aug 2019, 2:11 AM
Abdul Wahab
Abdul Wahab - avatar
5 Antworten
+ 10
getElementById selects an element by its id name while querySelector selects an element by tag name, id name and class name like this querySelector('tag') querySelector('.class') querySelector('#id')
19th Aug 2019, 2:17 AM
Farhan
Farhan - avatar
+ 7
So it is better to use querySelector rather than getElementById.
19th Aug 2019, 2:34 AM
Abdul Wahab
Abdul Wahab - avatar
+ 2
querySelector has more options where as getElementById is specific to id defined. These are not supported in IE6-7 if i am not mistaken.
20th Aug 2019, 5:13 AM
\•/
\•/ - avatar
+ 2
I find this useful to check browser compatibility. www.caniuse.com
20th Aug 2019, 5:17 AM
\•/
\•/ - avatar
+ 2
https://code.sololearn.com/WOx16Bf9u6KK/?ref=app https://code.sololearn.com/WvN162f7Czk8/?ref=app the query string of querySelector can be combined https://code.sololearn.com/WzVH8V29oi4H/?ref=app
21st Aug 2019, 12:34 AM
Gordon
Gordon - avatar