Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check a value exist in an array or not using jQuery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 256
    Comment on it

    Hello friends,

    Today we learn how to check a value exist an array or not.  In jQuery, $.inArray() method is used to check the value exist in an array or not. If value exist it will return the index of the value and if the value not exist in array it returns -1.

    Syntax:

    jQuery.inArray( value, array [, fromIndex ] )

    value: Value that is need to search in an array. Type can be anything

    array: It is an array from which value need to be search. Type is array

    fromIndex: This is the index from where search starts. By default, it's value is 0. That is, it will search the whole array. Type is number.

    For example:

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var myArray = [ 1, 2, 3, 5 ];
    if ( $.inArray( 4, myArray ) !== -1 ) {
       	$(".demo1").text("Value found in array");
    }
    else{
    	$(".demo1").text("Not found");
    }
    });
    </script>
    
    </head>
    <body>
    <div class="demo1"></div>
    
    </body>
    </html>
    

    In above example, it will display Not found on browser.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: