querySelector returns undefined string when trying to access numeric value as the ID of an element

StackOverflow https://stackoverflow.com/questions/18039889

Question

querySelector() returns undefined string when trying to access numeric value as the ID of an element. I would like to use querySelector() method over others, because it is more flexible. Any solutions to this problem would be a great help.

var foo = document.querySelector('#1');
console.log(foo);

jsFiddle

Was it helpful?

Solution

Weird .. Anyway this: document.querySelector('[id="1"]') is working. Fiddle: http://jsfiddle.net/wnd7K/2/

OTHER TIPS

Renaming the ids of the elements so that they begin with a letter solves your problem e.g. : <div id="a1"></div>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top