codeGK
Code it ALL
jQuery Selectors - CodeGK
♦ jQuery Selectors

♣ About jQuery Selectors

Selectors are typically used to select HTML element/s and perform certain tasks on the selected element/s.

♣ Example1: Element Selector in jQuery

The element selector selects the element based on the tag name of the HTML element.
For example selection of div,p,h1 tags can be done.
Below is an example of a HTML element h2 toggling.
Note that all the elements with the element name provide will be selected and the specified action will be performed on them.
Basic Syntax
Elements are selected by writing a simply the name of the element followed by element to be selected.
Below is the representation of Element Selector Syntax.
$("demo")
Syntax Explanation:
demo : the tag element for selection
In this example, .
Example
Input:
Output:

♣ Example2: Class Selector in jQuery

The class Selector selects the element with the given class attribute, there can be multiple elements with same class attributes in the same page.
Below is the representation of Class Selector Syntax.
Basic Syntax
$(".demo")
Syntax Explanation:
demo : class assosciated with tag element for selection
In this example, .
Example
Input:
Output:

♣ Example3: Id Selector in jQuery

The Id Selector selects by looking up element of required Id.
Note that an Id is unique to element in a page.
Also, note that as a consequence of which if the page has multiple elements with the same Id then the first element having that particular Id will be selected.
Below is the representation of Id Selector Syntax.
Basic Syntax
$("#demo")
Syntax Explanation:
demo : id assosciated with tag element for selection
In this example, .
Example
Input:
Output:
Prev_LessonNext_Lesson