how to get an element by query selector in javascript

To get an element by query selector in JavaScript, you can use the querySelector method of the document object. This method allows you to select an element based on a CSS selector. Here's an example:

index.tsx
// Get the first element that matches the CSS selector
const element = document.querySelector('#my-element');

// Do something with the element
element.classList.add('selected');
179 chars
6 lines

In this example, we use the querySelector method to select the element with the my-element ID, and then we add the selected class to it. You can use any valid CSS selector with querySelector.

gistlibby LogSnag