Getting elements using a CSS selector (not jQuery)
Learn how to get a DOM element via a CSS selector without using jQuery.
Until recently, I discovered that you can retrieve HTML DOM elements using CSS selectors. This can be done by calling document.querySelector('.your-class')
in your JS code. Previously, I was using jQuery to do this.
Here is an example:
Here’s what the HTML would look like, keeping it Vanilla JS:
You can see an example of this in CodePen here. Notice how when you load the page, you get the alert pop-up displaying “Hello, world!” This means the JavaScript is clicking the button.