jQuery Syntax & Selectors
Syntax for creating jQuery.

All jQuery method must be write inside document ready event.

$(document).ready(function(){
......
code
......
});

or

$(function(){
......
code
......
});

About jQuery Selector

Selector means select HTML element. by jquery selector. So first we select Html elements by Jquery selectors then perform operation on this like hide element etc. We select jquery element by id, classes, attributes, types etc that is from a DOM.

Syntax:-
$(selector).action()

jQuery selector.

1- element selector

2- id selector

3- class selector

Element selector - here p is html element

$(document).ready(function(){

$("button").click(function(){
$("p").hide();
});

});

Note : In this code when we click on button then paragraph will be hide

ID selector - here test is id of any html element

$(document).ready(function(){

$("button").click(function(){
$("#test").hide();
});

});

Note : In this code when we click on button then html element where id is test will be hide

ID selector - here test is class of any html element

$(document).ready(function(){

$("button").click(function(){
$(".test").hide();
});

});

Note : In this code when we click on button then html element where class is test will be hide

More Jquery Selectors

$("h1,div,p") - It will select all h1, div, and p elements.
$("p:first") - This will select the first p element
$(":input") - It will select all input elements
$(":text") - It will select all input elements with type="text"

Note: we create more selector like this.


Previous
Next