queryselector on tage javascript code example

Example 1: dom queryselector

// TO select all the h1 from Html
document.querySelectorAll("h1")

//To select h1 from a particular class or id
document.querySelector(".className/#id h1")

Example 2: document.queryselector

<div id="foo\bar"></div>
<div id="foo:bar"></div>

<script>
  console.log('#foo\bar');               // "#fooar" (\b is the backspace control character)
  document.querySelector('#foo\bar');    // Does not match anything

  console.log('#foo\\bar');              // "#foo\bar"
  console.log('#foo\\\\bar');            // "#foo\\bar"
  document.querySelector('#foo\\\\bar'); // Match the first div

  document.querySelector('#foo:bar');    // Does not match anything
  document.querySelector('#foo\\:bar');  // Match the second div
</script>

Tags:

Html Example