タグ

ブックマーク / kiru.io (1)

  • Difference between getElementByID and querySelector

    Assume you have a valid HTML element in DOM. I assumed the following snippets to be the same. let id = "037e3778-e157-4715-bff5-e466230fe7a3" const byId = document.getElementById(id) console.log(byId) // works const bySelectorConcat = document.querySelector("#" + id) console.log(bySelectorConcat) // Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#037e3778-e157-4715-bff5-e

    Difference between getElementByID and querySelector
    jay-es
    jay-es 2024/01/20
    数字で始まる id 属性は(正当な HTML だが)CSS セレクターとしては不正なので、単純に # をつけるだけだと document.querySelector で取得できない。`[id='${id}']` や `#${CSS.escape(id)}` にする必要がある
  • 1