jQuery Syntax


With jQuery, you can select or query HTML elements, and perform "actions" on them.

JQuery syntax

examples

$(this).hide() : Hide the current HTML elements.

$("#test").hide() : hidden id="test" elements.

$("p").hide() : hides all <p> elements.

$(".test").hide() : hide() function hides all the elements of class="test".

JQuery syntax is for the selection of HTML elements of the system, you can perform certain operations on the element.

The basic syntax is: $(selector).action()

  • The dollar sign defines jQuery
  • Selectors "Query" and "Find" HTML elements
  • JQuery's action() performs an operation on an element

Hint: The syntax used by jQuery is a combination of XPath and CSS selector syntax. You will learn more about the syntax of selectors in the following sections of this tutorial.


Document ready function

You may have noticed that all the jQuery functions in our example are in a document ready function:

$(document).ready(function(){
    //jQuery functions
});

This is to prevent the document from running jQuery code before it is fully loaded (ready).

Here is the shorthand with the same effect.

$(function(){
    //jQuery functions
});

If the function is run before the document is fully loaded, the operation may fail. Here are two specific examples:

  • Attempt to hide an element that does not exist
  • Gets the size of the image that is not fully loaded



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook