query selector class code example

Example 1: class query css js all

var matches = document.querySelectorAll("div.note, div.alert");

Example 2: jquery selector this and class

$(".class").click(function(){
     $(this).find(".subclass").css("visibility","visible");
});

Example 3: queryselector

var el = document.querySelector(".myclass");

Example 4: js queryselector names

// This selects the first element with that name
document.querySelector('[name="your-selector-name-here"]');

Example 5: queryselector function in javascript

/*The querySelector() method returns the first element that matches a specified 
CSS selector(s) in the document. Note: The querySelector() method only returns 
the first element that matches the specified selectors. To return all the 
matches, use the querySelectorAll() method instead.*/

// for example 

//for class
document.querySelector(".ClassName")

// for id 
document.querySelector("#ID")

// etc.

Example 6: 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:

Css Example