0

i am having the code like

<input type="checkbox" class="selectionne" id="first" value="1">
<input type="checkbox" class="selectionne" id="second" value="2">
    <input type="checkbox" class="selectionne" id="second" value="3">
        <input type="checkbox" class="selectionne" id="second" value="4">
            <input type="checkbox" class="selectionne" id="second" value="5">
                <input type="checkbox" class="selectionne" id="second" value="6">

jquery

var elems = [];
$('.selectionne').change(function(){
  this.checked ? elems.push($(this).val()) : elems.pop($(this).val());
  alert(elems);
});

Actually am pushing values to var elems by ckecking the checkboxes.pushing happening properly, But poping is happening like how i pushed values like 1,2,3,4,5 in reverse order its poping. i dont want like that, if i check randomly also it has to pop randomly only..

check here jsfiddle its working like wat i was explained. i want to pop randomly after unchecking check boxes....

plz help me out.. thanks in advance...

1
  • Note IDs must be unique Commented Jun 13, 2014 at 13:03

2 Answers 2

1

pop always removes the last element, it's the opposite of push. If you want to remove a specific element by value, use:

function removeValue(array, value) {
    array.splice(array.indexOf(value), 1);
};
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

var elems = [];
$('.selectionne').change(function(){
if(this.checked){
    elems.push(this.id) 
}
else {
    var toBeDeleted=this.id;  
    elems = jQuery.grep(elems, function(value) {
    return value != toBeDeleted; 
    });
    alert(elems);
}


});

http://jsfiddle.net/XBe89/

Hope this helps

Comments

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.