Reputation: 13
I am saving mobile numbers in array on check of checkbox and removing the mobile number when unchecked.
I tried this but it is not deleting the exact number which is unchecked.
var $this = $(this);
var index = parseInt($this.attr("_idx").slice(1));
console.log("index"+index);
if (index >=0 && index < recep.length) {
recep.splice(index, 1);
console.log("to2"+recep);
}
Upvotes: 1
Views: 1007
Reputation: 260
Try this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var mobileNoArray = [];
$(".mob-no").click(function(event){
var selectedNo = $(event.target).val();
var index = $.inArray(selectedNo, mobileNoArray);
if (index != -1) {// means found so pop it
mobileNoArray.splice(index, 1);
} else {
mobileNoArray.push(selectedNo);
}
console.log(mobileNoArray);
alert(mobileNoArray);
});
});
</script>
</head>
<body>
<label>
<input type="checkbox" value="9999999999" class="mob-no"/>
mob-1
</label>
<label>
<input type="checkbox" value="8888888888" class="mob-no"/>
mob-2
</label>
<label>
<input type="checkbox" value="7777777777" class="mob-no"/>
mob-3
</label>
</body>
</html>
Upvotes: 1