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>
Bookmarks