Categories
JavaScript Answers

How to use querySelector with IDs that are numbers with JavaScript?

Spread the love

Sometimes, we want to use querySelector with IDs that are numbers with JavaScript.

In this article, we’ll look at how to use querySelector with IDs that are numbers with JavaScript.

How to use querySelector with IDs that are numbers with JavaScript?

To use querySelector with IDs that are numbers with JavaScript, we can use the attribute value selector.

For instance, we write

const el = document.querySelector("[id='1']");

to select the element with id attribute set to 1.

We have to do this since numbers aren’t valid IDs in CSS.

Conclusion

To use querySelector with IDs that are numbers with JavaScript, we can use the attribute value selector.

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 *