Take the 2-minute tour ×

I am trying to code a fallback of getElementsByClassName with querySelectorAll for IE8.

The problem comes with classes which start with a number.

I know identifiers can't begin with a number, so querySelectorAll throws an error. But getElementsByClassName accepts them.

Then, is there a way of escaping those numbers?

share|improve this question
"In CSS2 .. to make ".55ft" a valid class, CSS2 requires the first digit to be escaped (".\35 5ft")" –  user2864740 Feb 8 at 20:58
Why? There are enough getElementsByClassName polyfills available, so don't reinvent the wheel! –  Bergi Feb 8 at 21:07
@Bergi Because they don't work like I want. And thanks for fixing the typo –  Oriol Feb 8 at 21:12
@JukkaK.Korpela The questions are almost the same, but I was referring to a JavaScript way of escaping (see I used javascript tag but the other question didn't). And maybe it won't be trivial to pass from other question's answers to a javascript approach, for someone who doesn't know JS very well. Then I'm not sure if it should be closed as duplicate. –  Oriol Feb 8 at 23:16
This is a specific implementation question, and, well, everything else you just said @Oriol. So I reopened it. –  BoltClock Feb 9 at 3:29

1 Answer 1

up vote 1 down vote accepted

I found the solution!

Identifiers can't start with a digit, but can start with an unicode escaped digit (see related answer).

Then, I can use

.replace(/\b\d/g, function(match){return '\\0000' + match.charCodeAt(0).toString(16);})

The code above escapes characters whose unicode code has two digits in hexadecimal. But for numbers, the following also works:

.replace(/\b\d/g, '\\00003$&')
share|improve this answer

Your Answer


