jQuery.inArray() method
Example
Return the index value of a specified element in an array
<
div
"John" was found at index value
<
span
spanwhere it was found
div
<
div
4 was found at index value
<
span
spanwhere it was found
div
<
div
"Karl" was 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
Try it »
Definition and Usage
The $.inArray() function is used to search for a specified value in an array and return its index value (or -1 if not found)
**Tip:** The source array is not affected; the filtering result is reflected only in the returned result array.
Syntax
$
.inArray( value, array [, fromIndex ] )
| Parameters | Description |
|---|---|
| value | Any type: The value to search for. |
| array | Array type: The array to search in. |
| fromIndex | Optional. Number type: Specifies the index position in the array from which to start searching, defaulting to 0 |
jQuery Miscellaneous Methods