Category Archives: Jquery Blog
jQuery find method
jQuery find() method is used to get all descendant elements of the selected element.
Syntax : jQuery find() method
$(selector).find(filter);
filter:
– An element or string which contains the selector expression to match the elements.
jQuery find method Example
jQuery filter method
jQuery filter method() is used to get elements that match the specified pattern.
Syntax : jQuery filter method
$(selector).filter(criteria,function(index));
Criteria:
– Selector expression to match the current set of elements.
Function:
– Function used to run for each elements. this represents current DOM.
jQuery filter method Example
jQuery eq method
jQuery eq method() finds the element at specified index.
Syntax of jQuery eq Method
$(selector).eq(index);
Index :
– Index of element can be positive or negative number.
jQuery eq method Example 1
jQuery each method
jQuery each method() uses a function to iterate for each matching element. ie. function executes for each matching element.
Syntax of jQuery each Method
$(selector).each(function(Integer index, Element element ));
Function :
– Index – Current Index of selector.
– element Current element.
Let us understand it very basic example :
Suppose We have following elements-
- Item 1
- Item 2
- Item 3
Let us select all li items and iterate them-
$( "li" ).each(function( index, elem) { console.log( index + ": " + $( elem ).text() ); });
or
$( "li" ).each(function( index ) { console.log( index + ": " + $( this ).text() ); });
The following message will be logged-
0: Item 1
1: Item 2
2: Item 3
jQuery each method Example 1
jQuery contents Method
Get children of each element including text and comment nodes.
Syntax : jQuery contents() Method
$(selector).contents();
Note : It does not requires any argument.
jQuery contents Method Example
jQuery closest Method
jQuery closest Method :It is used to get first ancestor element of the current selection. If you want to get first ancestor element of the selected element use closest method which accepts filter as input parameter.
Syntax of jQuery closest Method
Here is syntax for closest Method example in jQuery –
$(selector).closest(filter);
filter String containing the selector expression for matching the elements.
jQuery Traversing – closest Example
The above code will produce following result-
Note : This is image of the output. To run this demo click on the above “Try it” button.
jQuery children() Method
Get all the elements of the selected element.
Syntax : jQuery Traversing – children() Method
$(selector).children([selector element]);
[selector element] String containing the selector expression for matching the elements.
jQuery Traversing – children() Example 1
jQuery Traversing – children() Example 2
jQuery addBack() Method
jQuery addBack() Method Adds the previous set of elements to the current set.
Syntax of jQuery Traversing – addBack() Method
$(selector).addBack([selector element]);
jQuery add() Method
jQuery add Method : Adds elements to the matched elements.
Syntax of jQuery Traversing – add() Method
$(selector).add(element,context);
Parameters :
element : One or more elements to be added in the matching selections.
context : Is optional. It is point of document from where selector should begin the matching.