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()

We can also In jQuery there are mainly three selector.

1- element selector

2- id selector

3- class selector

By element selector we can select any HTML element

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

Note : In this code we select paragraph and hide paragraph

By id selector we can select any HTML element

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

Note : In this code we select html element where id is test, then we hide

By class selector we can select any HTML element

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

Note : In this code we select html element where class is test, then we 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