Skip to content

Comment on JavaScript: Difference Between GetElementByID and QuerySelectorparent

Comments

Interesting difference but why is querySelector(`#${CSS.escape(id)}`) better?

The linked SO[0] link answers the question: Basically, you can have an id starting with a number, but have to escape them correctly.

I was mostly surprised to learn there is a difference between CSS selectors and the querySelector in Javascript.

[0] https://stackoverflow.com/a/20306237

I'm mostly surprised we have different rules for id's in html5 and css.

That you can hack something up that will accept numbers is no surprise. You could use getAttribute or xpath, I'm sure those will accept numbers too.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.