Check if value exists in array jquery

Introduction:In this tutorial I am going to discuss about to check if value exists in array or not using jquery. The whole process done under the  <script></script> tag. Let's see.
Steps:
1.Make a function name like valexixts.
2.Declare a 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>



Previous
Next Post »