query selector all by id in javascript

To select an element by its id attribute in JavaScript, you can use the querySelectorAll method with the # symbol followed by the id name.

Here's an example code block:

index.tsx
const myElement = document.querySelectorAll('#myId');
54 chars
2 lines

This code selects all the elements with an id attribute equal to myId.

Note that querySelectorAll returns a NodeList object, which is an array-like object that contains all elements that match the specified selector. If you want to select only the first matching element, you can use the querySelector method instead:

index.tsx
const myElement = document.querySelector('#myId');
51 chars
2 lines

gistlibby LogSnag