I have heard that querySelector and querySelectorAll are new methods to select DOM elements. How do they compare to the older methods, getElementById and getElementsByClassName in terms of performance and browser support?
How does the performance compare to using jQuery's query selector?
What is the difference between querySelector and getElementById? When should we use querySelector instead of getElementById? Is there any example which is not possible using getElementById?
getElementByIdandgetElementsByClassNameare still ideal for the purposes their names describe.qS/qSAcan be used from any element context, butgEBIcan only be used from thedocumentcontext.getElementByIdmatches theidattributes to find DOM nodes, whilequerySelectorsearches by selectors. So for an invalid selector e.g<div id="1"></div>,getElementById('1')would work whilequerySelector('#1')would fail, unless you tell it to match theidattribute (e.gquerySelector('[id="1"]').querySelectorandquerySelectorAllare fully supported now. caniuse.com/#feat=queryselector