jQuery Syntax

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

  • $ sign to define/access jQuery
  • (selector) to "query (or find)" HTML elements
  • jQuery action() to be performed on the element(s)

jQuery(this).hide() - hides the current element. Shorter code replace jQuery with $

$(this).hide() - hides the current element.

$("div").hide() - hides all DIV elements.

$(".someclass").hide() - hides all elements with class="someclass".

$("#mainbutton").hide() - hides the element with id="mainbutton".

The Document Ready Event

This is to prevent any jQuery code from running before the document is finished loading (is ready).
 // write code here

The short hand for the above code is:

  // write code here
The code shown below is a self-invoking anonymous JavaScript function, and will be executed as soon as browser interprets it:
  // write code here
})();   // It is the parenthesis here that call the function.
The jQuery self-invoking function shown below, passes the global jQuery object as an argument to function($). This enables $ to be used locally within the self-invoking function without needing to traverse the global scope for a definition. jQuery is not the only library that makes use of $, so this reduces potential naming conflicts.
  // write code here