from Hacker News

JavaScript: Difference Between GetElementByID and QuerySelector

by kiru_io on 1/16/24, 10:48 AM with 3 comments

  • by 6510 on 1/16/24, 12:16 PM

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

    I would prefer to use valid id's but also, when selecting by id I just use getElementByID?

    You could also use querySelectorAll(id) and have more than one element with the same id. It doesn't seem like a good idea.