4 is found at">
4 is found at" />
Easy Tutorial
❮ Misc Callbacks Lock Traversing Parentsuntil ❯

jQuery.inArray() Method

jQuery Miscellaneous Methods

Example

Returns the index of the specified element in the array.

<div>"John" is found at index <span></span></div>
<div>4 is found at index <span></span></div>
<div>"Karl" is not found, so it returns <span></span></div>
<div>"Pete" is in the array, but not at an index greater than 2, so it returns <span></span></div>
<script>
$(function () { 
    var arr = [ 4, "Pete", 8, "John" ];
    var $spans = $( "span" );
    $spans.eq( 0 ).text( jQuery.inArray( "John", arr ) );
    $spans.eq( 1 ).text( jQuery.inArray( 4, arr ) );
    $spans.eq( 2 ).text( jQuery.inArray( "Karl", arr ) );
    $spans.eq( 3 ).text( jQuery.inArray( "Pete", arr, 2 ) );
})
</script>

Definition and Usage

The $.inArray() function is used to find the specified value in an array and returns its index (or -1 if not found).

Note: The source array is not affected; the filtered results are only reflected in the returned result array.


Syntax

Parameter Description
value Any type The value to search for.
array Array type The array to search in.
fromIndex Optional. Number type The index at which to start the search, default is 0.

jQuery Miscellaneous Methods

❮ Misc Callbacks Lock Traversing Parentsuntil ❯