Sometimes, we want to use querySelector with IDs that are numbers with CSS and JavaScript.
In this article, we’ll look at how to use querySelector with IDs that are numbers with CSS and JavaScript.
How to use querySelector with IDs that are numbers with CSS and JavaScript?
To use querySelector with IDs that are numbers with CSS and JavaScript, we’ve to escape it.
For instance, we write
#\31 {
background: hotpink;
}
to select the element with ID 1 by putting \3 before the ID.
Then we set its background.
In JavaScript, we write
const el = document.getElementById("1");
or
const el = document.querySelector("#\\31 ");
to select the element with ID 1 with getElementById
or querySelector
.
Conclusion
To use querySelector with IDs that are numbers with CSS and JavaScript, we’ve to escape it.