ItSolutionStuff.com

JQuery Check If Value Exists or Not in Array Example

By Hardik Savani November 5, 2023
jQuery

If you need to check if value exists in jquery array then you can do it using jquery inarray function. $.inArray will help you to find and check if value is exists or not.

$.inArray function will return the index of element. If element not exist in given array it will return -1. So, we can check it very simply weather a value exist in array or not.

You can see bellow simple example how you can check with if condition:

Example:

<!DOCTYPE html>

<html>

<head>

<title>Jquery check if value exists in Array Example - ItSolutionStuff.com</title>

<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>

</head>

<body>

<script type="text/javascript">

var myArray = ['PHP', 'Laravel', 'Codeigniter'];

console.log($.inArray('Laravel', myArray));

if ($.inArray('Laravel', myArray) >= 0) {

console.log('Laravel is exist!');

}else {

console.log('Laravel does not exist!');

}

</script>

</body>

</html>

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Split String to Array By Comma using JQuery?

Read Now →

How to Remove Empty or Null Values from JSON using JQuery?

Read Now →

Add/remove multiple input fields dynamically with Jquery Laravel 5.8

Read Now →

How to Check If Key Exists in JSON Object using JQuery?

Read Now →

How to Get Selected Checkbox Value from Checkboxlist using JQuery?

Read Now →

JQuery Rotate Image Animation Example Code

Read Now →

JQuery Reload Page After 5 Seconds Example

Read Now →

PHP JQuery Ajax Image Upload Example Tutorial

Read Now →

How to Remove Comma from String in JQuery?

Read Now →

JQuery Timepicker with AM PM Format Example

Read Now →

How to Each Loop with Class Element in JQuery?

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to Convert Line Breaks to br in jQuery ?

Read Now →

How to Check Object is Empty or Not in JQuery?

Read Now →