Jquery inArray check if value exists in Array

Jquery inArray check if value exists in Array

Jquery inArray check if value exists in Array

jQuery inArray Example

jQuery inArray() Method is used to check if value exists in Array and return its index.

Syntax:

jQuery.inArray( search-value, value-from-which-search);

If you need to check the value in the jQuery array you can do it using the jQuery inarray function function. jQuery inarray function will help you find and check whether a value exists or not.

jQuery inarray function will return the element index. If the item is not in the given sequence it will return -1. Therefore, we can easily check the weather whether the value is in sequence or not.

Read Also :  Show Hide Div using Next Previous Button in jQuery

You can see a very simple example of how to look when you have a condition:

Jquery inArray check if value exists in Array

Example : 1 jQuery inarray function

<!DOCTYPE html>
<html>
<head>
<title>jQuery inArray check if value exists in Array</title>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>

<script type="text/javascript">

var myArray = ['React', 'jQuery', 'Laravel'];

console.log(jQuery.inArray('jQuery', myArray));

if (jQuery.inArray('jQuery', myArray) >= 0) {
console.log('jQuery is exist!');
}else {
console.log('jQuery does not exist!');
}

</script>
</body>
</html>

Example : 2 jQuery inarray function

<!DOCTYPE html>
<html>
<head>
<title>jQuery inArray check if value exists in Array</title>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
</head>
<body>
<script type='text/javascript'>
var names_arr = ['dharmik','sanket','fenil','ved','shlok'];
var text = "ved patel";
// Searching in Array
console.log( 'Index : ' + jQuery.inArray('ved', names_arr) );
console.log( 'Index : ' + jQuery.inArray('dharmik', names_arr ));

// Searching in String variable
console.log( 'Index : ' + jQuery.inArray( "e", text ));

</script>
</body>
</html>