Results 1 to 2 of 2

Thread: check if element is in array using jquery

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default check if element is in array using jquery

    i have left/right buttons, the right button pushes elements into an array.
    if the user pushes the left button then pushes the right button, i want to check the right number against the array.
    heres my code so far, any help would be greatly appreciated
    Code:
    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
    <style type="text/css">
    div{float:left; margin-right:50px;}
    </style>
    <script src="http://code.jquery.com/jquery.min.js"></script>
    <script type="text/javascript">
    var num = 0;
    var arr = [];
    $(function(){
    	$('#btn1').click(function() {
    		num--;
    		$('#rec').html(num);
    		return false;
    	});
    	
    	$('#btn2').click(function() {
    		num++;
    		arr.push(num);
    		$('#rec').html(num);
    		
    		if(  jQuery.inArray(num, arr)  ){// doesnt work
    		// do nothing
    		}else{
    			$('#arrayRec').html(arr.join(','));
    		}
    		return false;
    	});
    });
    </script>
    </head>
    <body>
    <div id="btn1"><a href="">left</a></div>
    <div id="btn2"><a href="">right</a></div>
    <div id="rec">0</div>
    <div id="arrayRec">null</div>
    </body>
    </html>
    Last edited by ggalan; 02-12-2012 at 03:43 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    jQuery.inArray(item, array) returns the index of the item in the array or -1 if the item is not present. So your test:

    Code:
    if(  jQuery.inArray(num, arr)  )
    should be:

    Code:
    if(  jQuery.inArray(num, arr) > -1 )
    Which will return true if the number num is in the array arr. And since it always is, nothing will ever be sent to the arrayRec div.

    A better illustration of the utility of inArray might be:

    Code:
    <script type="text/javascript">
    var num = 0;
    var arr = [];
    $(function(){
    	$('#btn1').click(function() {
    		num--;
    		$('#rec').html(num);
    		return false;
    	});
    	
    	$('#btn2').click(function() {
    		num++;
    		$('#rec').html(num);
    
    		if( jQuery.inArray(num, arr) < 0 ){ // if the number isn't already in the array
    			arr.push(num); // put it in the array and
    			$('#arrayRec').html(arr.join(',')); // update the report of the array's contents
    		}
    		return false;
    	});
    });
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ggalan (02-12-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •