Syntax
The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element(s).
The basic syntax is:
$(selector).action()
- A dollar sign ( $ ) to define jQuery
- A (selector) to "find" HTML elements
- A jQuery action() to be performed on the selected element(s)
Example 1: Hides the current element
$(this).hide()
Example 2: Hides all paragraphs
$("p").hide()
Example 3: Hides all paragraphs with class="test"
$("p.test").hide()
Example 4: Hides the element with id="test"
$("#test").hide()
The Document Ready Function
Many jQuery actions can fail if they are run before the document is fully loaded. To prevent this, it is recommended to wrap all jQuery functions in a document ready function as follows:
$(document).ready(function(){
// jQuery functions go here...
});
This will ensure that jQuery functions won't fail due to unloaded pages.