Check if value exists in array jquery - Solved

Introduction: In this tutorial, I am going to discuss about to check if the value exists in an array or not using jquery. The whole process done under the  <script></script> tag. Let's see. Check if the value already exists in array jquery 


Steps:
1. Make a function name like valexixts.
2. Declare an array[] of var type.
3. Add Jquery Library CDN link.

Coding Part:


<html>
<head>
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
        function valexixts() {          
            var items = ['bat''ball''shirt''chair' 'fridge''tv'];
            var item = $('#txtItem').val();
if ($.inArray(item, items) != -1) 
            {
                $('#lblmsg').html(item + ' found');
            }
            else {               
                $('#lblmsg').html(item + ' not found');
            }
        }
    </script>
</head>
<body>
    <input type="text" id="txtcheck" />
       <button type="button" onclick="valexixts();">Check</button><br />
 <span id="lblmsg"></span>
</body>
</html>



Post a Comment

Post a Comment (0)

Previous Post Next Post