What are Selectors in jQuery?

jQuery selectors allow you to select and manipulate HTML element(s).

jQuery selectors are used to “find” (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes, and much more. It’s based on the existing CSS Selectors, and in addition, it has its own custom selectors.

All selectors in jQuery start with the dollar sign and parentheses: $().

Elements Selector : The elements selector selects the element on the basis of its name.

$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});

Id Selector : The id selector selects the element on the basis of its id.

$(document).ready(function(){
  $("button").click(function(){
    $("#test").hide();
  });
});

Class Selector : The class selector selects the element on the basis of its class.

$(document).ready(function(){
  $("button").click(function(){
    $(".test").hide();
  });
});
Try it Yourself »