-4

I wanted to insert value in if condition and remove it in else condition.

$(document).ready(function(){
    var ids=[];
    $('input[type="checkbox"]').click(function(){
        if($(this).prop('checked')){
            var id = $(this).val();
            ids.push(id)
        }
        else
        {
           //remove value from array
        }


    });

});
Chitrank Samaiya
  • 780
  • 9
  • 20
  • jQuery is just JavaScript. There is nothing special going on here, except the improper use of a parameter (which does not evaluate to an array). Minimally reproducible with: `"notanarray".push("foo")`. – user2864740 Sep 18 '14 at 06:43

2 Answers2

1

Instead of :

array.push(id)

It should be :

ids.push(id)

(because your array name is ids not array)

Kartikeya Khosla
  • 18,039
  • 8
  • 39
  • 64
0

Click event's callback function parameter is event

$('input[type="checkbox"]').click(function(array){

"array" is instance of event on above code.

Then variable "array" cannot have function "push".

easywaru
  • 963
  • 8
  • 16
  • What are you talking about? The OP is not using the event parameter of the handler, so I have no idea what you're getting at. The event object has no `push` method anyway, so this will just cause an error. – Rory McCrossan Sep 18 '14 at 07:07
  • This Question is edited by User. Code is like above on first time – easywaru Sep 18 '14 at 07:18