JQuery | inArray() method

22 Apr, 2020
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>          <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>   <body style="text-align:center;">           <h1 style="color: green">         GeeksForGeeks     </h1>           <p id="GFG_UP"></p>          <button onclick="gfg_Run()">         Click Here     </button>           <p id="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>
<!DOCTYPE HTML> <html>  <head>     <title>         JQuery | inArray() method    </title>          <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>   <body style="text-align:center;">           <h1 style="color: green">         GeeksForGeeks     </h1>           <p id="GFG_UP"></p>          <button onclick="gfg_Run()">         Click Here     </button>           <p id="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>      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>   <body style="text-align:center;">       <h1 style="color: green">         GeeksForGeeks     </h1>       <p id="GFG_UP"></p>      <button onclick="gfg_Run()">         Click Here     </button>       <p id="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>
<!DOCTYPE HTML> <html>  <head>     <title>         JQuery | inArray() method    </title>      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">    </script></head>   <body style="text-align:center;">       <h1 style="color: green">         GeeksForGeeks     </h1>       <p id="GFG_UP"></p>      <button onclick="gfg_Run()">         Click Here     </button>       <p id="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:
jQuery-Methods
JavaScript
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
How to fetch data from JSON file and display in HTML table using jQuery ?