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.