Arrays in jQuery

check if an element exists within an array

by Joe Saad

HTML

<div id="place">checking if an element is within an array or not</div>

JavaScript

$(document).ready(function(){

    var arr = [ "Adam", "Pete", "Samson", "John" ];   
    
    alert($.inArray("John", arr));
   

    if ($.inArray("John", arr)>-1) {
        $('#place').text('found');
        //alert('found');
    }
    else{
        //alert('not found');
        $('#place').text('NOT found');
    }


});