Tag Archives: jquery tutorial with examples for beginners

jQuery map method


jQuery “last()” method passes each element in current matched set using a function which generates new jQuery objects and returns value.


Syntax : jQuery map() method

$(selector).map(function(index,element));

index : Index of current element.
element : Current element.

jQuery map Method Example

jQuery map() Method – Example


Try it »

jQuery last method


jQuery “last()” method is used to get the last element of the selected elements.


Syntax : jQuery last() method

$(selector).last();

Note : It accepts no parameter.

jQuery last method Example

jQuery last() Method with Syntax


Try it »

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 find() Method Example with Syntax


Try it »

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 filter() Method Example with syntax

Try it »

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 eq() Method – Example


Try it »

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 each() Method – Example


Try it »

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 contents() Method Example with Syntax


Try it »

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

jQuery closest() Method – Example

Try it »

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 closest Method

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 children() Method with syntax


Try it »

jQuery Traversing – children() Example 2

jQuery children() Method with syntax


Try it »