JavaScript’s querySelector
Sunday, September 2nd, 2012 | Programming
jQuery has an amazing set of selectors, but what happens if you don’t have access to jQuery?
It happens, especially if you’re writing the automated tests or working within an existing framework and don’t necessarily have control over the content of the page. Luckily, you don’t actually need jQuery!
All modern browsers support the use of querySelector. When I saw all modern browsers, I even include Internet Explorer which added basic support for it in 8 and full support in 9. You use it like this.
document.querySelector(“p.introduction”);
This works just like jQuery – selecting all paragraph tags with the class introduction.
jQuery has an amazing set of selectors, but what happens if you don’t have access to jQuery?
It happens, especially if you’re writing the automated tests or working within an existing framework and don’t necessarily have control over the content of the page. Luckily, you don’t actually need jQuery!
All modern browsers support the use of querySelector. When I saw all modern browsers, I even include Internet Explorer which added basic support for it in 8 and full support in 9. You use it like this.
document.querySelector(“p.introduction”);
This works just like jQuery – selecting all paragraph tags with the class introduction.