Categories
JavaScript Answers

How to use getElementById with multiple IDs in JavaScript?

Spread the love

To use getElementById with multiple IDs in JavaScript, we use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll(
  "#myCircle1, #myCircle2, #myCircle3, #myCircle4"
);

to call querySelectorAll with the selector for the elements we want to select separated by commas.

A node list with all the selected elements is returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *