1

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);
}

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

searched in stackoverflow for long and finally found one which works like a charm....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.