jQuery syntax was designed to allow developers to easily select DOM element(s) and then perform some action on the element(s). The rudimentary syntax is as follows:
Basic syntax |
Copy Code |
---|---|
$(selector).action() |
The $ sign references jQuery, (selector) queries the DOM element or elements, and .action() performs an action on the element.
Examples of jQuery syntax:
$(this).hide()
hides the current element.
$("p").hide()
hides all paragraphs.
$("p.wow").hide()
hides all paragraphs of the class "wow".
$("#wow").hide()
hides an element with the id of "wow". Before you start using jQuery, it’s crucial to understand the concept of jQuery selectors. jQuery selectors utilize CSS syntax, thus allowing developers to precisely select elements to apply effects to. Using these selectors, you can select specific DOM elements or groups of elements by attribute name, tag name, ID, or even by content. The different types of selectors are as follows:
jQuery Element Selectors
In jQuery, you can use CSS selectors to choose specific DOM elements. For example:
$(this)
selects the current DOM element.
$("div")
selects all <div> elements.
$(".super")
selects all elements of the class "super".
$("div.super")
selects all <div> elements of the class "super".
$("#wow")
selects the first element with the id "wow".
$("div#wow")
selects the first <div>element with the id "wow". jQuery Attribute Selectors
If you’d like to select an element by attribute rather than by DOM elements, you can use Xpath expressions to select elements with a specific attribute. For example:
$("[href]")
selects all elements with an href attribute.
$("[href="#"]")
selects all elements with an href attribute with a value equal to "#".
$("[href!="#"]")
selects all elements with an href attribute with a value that is not equal to "#". jQuery CSS Selectors
If you want to change the CSS property for a DOM element, you can use a CSS selector. For example, the following jQuery script changes the background color of all <div> elements to red:
Script for the head section |
Copy Code |
---|---|
$("div").css("background-color","red"); |
To prevent jQuery code from running before the document is completely loaded, all jQuery functions should be placed within the $(document).ready function
. For example:
To create a document ready function
Paste inside the script tag |
Copy Code |
---|---|
$(document).ready(function(){ // Do something on document ready. }); |
To use a shortened version of the document ready function
Paste insidse the script tag |
Copy Code |
---|---|
$(function() { // do something on document ready }); |
All scripts residing inside of the $(document).ready
function will load as soon as the DOM is loaded and before the page contents are loaded.
jQuery options are properties passed into a widget as arguments. Each widget option has a default configuration. You can override these defaults to customize the widget.
For example, assume that the wijprogressbar widget has a maxValue option with a default value of 100, but you want this value to be 85. Changing the maxValue option from its default to a value of 85 is as simple as passing an argument to the wijprogressbar:
To pass a maxValue argument to wijprogressbar
Paste inside the script tag |
Copy Code |
---|---|
$(‘#progressbar’).wijprogressbar({ maxValue: 85 }); |
With the maxValue option set to 85, the range of values on the wijprogressbar widget is 0 – 85, as the minValue property is set to 0 by default. If you want to change the minValue option to 25, just add a comma after the maxValue argument and then write your minValue argument:
To pass a second argument
Paste inside the script tag |
Copy Code |
---|---|
$(‘#progressbar’).wijprogressbar({ maxValue: 85, minValue: 25 }); |
You can pass as many options to the widget as you want – just remember your commas!