put multiple elements by id in one const in javascript in javascript

To select and store multiple elements by their id in one const variable in JavaScript, you can use the document.querySelectorAll() method, which returns a NodeList of all elements that match a certain CSS selector.

You can select elements based on their id using the # selector. To select multiple elements by their id, you can use the , separator. Here's an example:

index.tsx
const elements = document.querySelectorAll('#element1, #element2, #element3');
79 chars
2 lines

This will select all elements with id "element1", "element2", and "element3", and store them in the "elements" constant.

You can then use this constant to perform actions on all the selected elements at once, using a loop or other methods. For example:

index.tsx
elements.forEach(element => {
  // perform action on each element
});
70 chars
4 lines

gistlibby LogSnag