This inArray() Method in jQuery is used to search for a specific value in an array and return its index (or -1 if not found).
Syntax:
jQuery.inArray(val, arr [, Index])
Parameters: The inArray() method accepts few parameters that are mentioned above and described below:
- val: The value to search in an array.
- arr: Any array like object.
- Index: Index of the array from which to begin search.
Return Value: It returns the index of the element in an array.
- Example 1: In this example, the inArray() method searches the element ‘geek’ in the array.
<!DOCTYPE HTML>Â<html>ÂÂ<head>   Â<title>       ÂJQuery | inArray() method   Â</title>    Â   Â<scriptsrc=   Â</script></head>ÂÂÂ<bodystyle="text-align:center;">    Â   Â<h1style="color: green">       ÂGeeksForGeeks   Â</h1>    Â   Â<pid="GFG_UP"></p>    Â   Â<buttononclick="gfg_Run()">       ÂClick Here   Â</button>    Â   Â<pid="GFG_DOWN"style="color:green;"></p>    Â   Â<script>       Âvar el_up = document.getElementById("GFG_UP");       Âvar el_down = document.getElementById("GFG_DOWN");        Â       Âvar arr = ["GFG", "GeeksForGeeks", "Geek", "Geeks"];       Âvar val = "Geek";        Â       Âel_up.innerHTML = "Click on the button to "                       Â+ "search the element.<br>"                       Â+ "Array - [" + arr +                       Â"]<br>Element - '" + val + "'";        Â       Âfunction gfg_Run() {           Âel_down.innerHTML = $.inArray(val, arr);       Â}   Â</script>Â</body>ÂÂÂ</html> - Output:
- Example 2: In this example, the inArray() method searches the element ‘geek’ in the array as in the previous example. But, index attribute is also passed to search the element from index 3 and return -1.
<!DOCTYPE HTML>Â<html>ÂÂ<head>   Â<title>       ÂJQuery | inArray() method   Â</title>   Â<scriptsrc=   Â</script></head>ÂÂÂ<bodystyle="text-align:center;">   Â<h1style="color: green">       ÂGeeksForGeeks   Â</h1>   Â<pid="GFG_UP"></p>   Â<buttononclick="gfg_Run()">       ÂClick Here   Â</button>   Â<pid="GFG_DOWN"style="color:green;"></p>   Â<script>       Âvar el_up = document.getElementById("GFG_UP");       Âvar el_down = document.getElementById("GFG_DOWN");        Â       Âvar arr = ["GFG", "GeeksForGeeks", "Geek", "Geeks"];       Âvar val = "Geek";       Âel_up.innerHTML = "Click on the button to search"               Â+ "the element.<br>Array - [" + arr +               Â"]<br>Element - '" + val + "'";                Â       Âfunction gfg_Run() {           Âel_down.innerHTML = $.inArray(val, arr, 3);       Â}   Â</script>Â</body>ÂÂ</html> - Output:
