Please disable your adblock and script blockers to view this page
 

jQuery to check whether value exists in array or not?

Introduction: In this article I am going to explain how to find whether specified value/item exists in array or not using jQuery’s $.inArray() or javascript’s indexOf() methods. 

jQuery to check whether value exists in array or not?

Description: The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match and returns the index of the element if found.

Implementation: Let’s understand by an example.

<html>
<head>
    <title></title>
<script type="text/javascript">
        function CheckItem() {          
            var items = ['chair', 'table', 'carpet', 'fan', 'cooler', 'almirah', 'oven', 'tv'];
            var item = $('#txtItem').val();
         
if ($.inArray(item, items) != -1) // or if (items.indexOf(item) != -1)
            {
                $('#spnMessage').html(item + ' found');
            }
            else {               
                $('#spnMessage').html(item + ' not found');
            }
        }
    </script>
</head>
<body>
    <input type="text" id="txtExpression" />
       <button type="button" onclick="CheckItem();">Submit</button><br /><br />
 <span id="spnMessage"></span>
</body>

</html>

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Newest
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon