JQuery Syntax

jQuery statement typically starts with the dollar sign ($) and ends with a semicolon (;). In jQuery, the dollar sign ($) is just an alias for jQuery and it is used for selecting HTML elements and perform some action on the element(s).

Basic syntax is: $(selector).action()

Examples:

  • $(this).hide() - hides the current element.
  • $("p").hide() - hides all <p> elements.
  • $(".element").hide() - hides all elements with class="element".
  • $("#element").hide() - hides the element with id="element".

#id Selector

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.

Below the code of find an element with a specific id.

$("#element")

Example

when a user clicks on a button, the element with class="element" will be hidden:

Below an example of hide the class="element"

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

.class Selector

The jQuery .class selector uses the class attribute of an HTML tag to find the specific element.

Below the code of find an element with a specific class.

$(".element")

Example

when a user clicks on a button, the element with class="element" will be hidden:

Below an example of hide the class="element"

$(document).ready(function() {
   $("button").click(function() {
      $(".element").hide();
   });
});
More Examples of jQuery Selectors
Selector Example Description
* $("*") It is used to select all elements.
#id $("#firstname") It'll select the element with id="firstname"
.class $(".primary") It'll select all elements with class="primary"
class,.class $(".primary,.secondary") It'll select all elements with the class "primary" or "secondary"
element $("p") It'll select all p elements.
el1,el2,el3 $("h1,div,p") It'll select all h1, div, and p elements.
:first $("p:first") This'll select the first p element
:last $("p:last") This'll select he last p element
:even $("tr:even") This'll select all even tr elements
:odd $("tr:odd") This'll select all odd tr elements
:first-child $("p:first-child") It'll select all p elements that are the first child of their parent
:first-of-type $("p:first-of-type") It'll select all p elements that are the first p element of their parent
:last-child $("p:last-child") It'll select all p elements that are the last child of their parent
:last-of-type $("p:last-of-type") It'll select all p elements that are the last p element of their parent
:nth-child(n) $("p:nth-child(2)") This'll select all p elements that are the 2nd child of their parent
:nth-last-child(n) $("p:nth-last-child(2)") This'll select all p elements that are the 2nd child of their parent, counting from the last child
:nth-of-type(n) $("p:nth-of-type(2)") It'll select all p elements that are the 2nd p element of their parent
:nth-last-of-type(n) $("p:nth-last-of-type(2)") This'll select all p elements that are the 2nd p element of their parent, counting from the last child
:only-child $("p:only-child") It'll select all p elements that are the only child of their parent
:only-of-type $("p:only-of-type") It'll select all p elements that are the only child, of its type, of their parent
parent > child $("div > p") It'll select all p elements that are a direct child of a div element
parent descendant $("div p") It'll select all p elements that are descendants of a div element
element + next $("div + p") It selects the p element that are next to each div elements
element ~ siblings $("div ~ p") It selects all p elements that are siblings of a div element
:eq(index) $("ul li:eq(3)") It'll select the fourth element in a list (index starts at 0)
:gt(no) $("ul li:gt(3)") Select the list elements with an index greater than 3
:lt(no) $("ul li:lt(3)") Select the list elements with an index less than 3
:not(selector) $("input:not(:empty)") Select all input elements that are not empty
:header $(":header") Select all header elements h1, h2 ...
:animated $(":animated") Select all animated elements
:focus $(":focus") Select the element that currently has focus
:contains(text) $(":contains('foo')") Select all elements which contains the text "foo"
:has(selector) $("div:has(p)") Select all div elements that have a p element
:empty $(":empty") Select all elements that are empty
:parent $(":parent") Select all elements that are a parent of another element
:hidden $("p:hidden") Select all hidden p elements
:visible $("table:visible") Select all visible tables
:root $(":root") It'll select the document's root element
:lang(language) $("p:lang(en)") Select all p elements with a lang attribute value starting with "en"
[attribute] $("[href]") Select all elements with a href attribute
[attribute=value] $("[href='default.html']") Select all elements with a href attribute value equal to "default.html"
[attribute!=value] $("[href!='default.html']") It'll select all elements with a href attribute value not equal to "default.html"
[attribute$=value] $("[href$='.jpg']") It'll select all elements with a href attribute value ending with ".jpg"
[attribute|=value] $("[title|='Tomorrow']") Select all elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen
[attribute^=value] $("[title^='John']") Select all elements with a title attribute value starting with "John"
[attribute~=value] $("[title~='hello']") Select all elements with a title attribute value containing the specific word "hello"
[attribute*=value] $("[title*='hello']") Select all elements with a title attribute value containing the word "hello"
:input $(":input") It'll select all input elements
:text $(":text") It'll select all input elements with type="text"
:password $(":password") It'll select all input elements with type="password"
:radio $(":radio") It'll select all input elements with type="radio"
:checkbox $(":checkbox") It'll select all input elements with type="checkbox"
:submit $(":submit") It'll select all input elements with type="submit"
:reset $(":reset") It'll select all input elements with type="reset"
:button $(":button") It'll select all input elements with type="button"
:image $(":image") It'll select all input elements with type="image"
:file $(":file") It'll select all input elements with type="file"
:enabled $(":enabled") Select all enabled input elements
:disabled $(":disabled") It'll select all disabled input elements
:selected $(":selected") It'll select all selected input elements
:checked $(":checked") It'll select all checked input elements