4

I want to remove particular value from the array. I have written code like:

   $(".remove", document.getElementById("TXT")).live("click", function () {      
          $(this).parent().remove();
          var removeitem = $(this).parent().attr('id');                         
          pushvar.splice($.inArray(removeitem, pushvar), 1);

      });

In the above code pushvar is an array. Suppose if it contains 3 elements.The function will repeat for three times when we click on one of the remove button. For eg the pushvar contains [5,6,7] elements. If i click on the remove button of 6. Then the function will repeat for three times. But the

 pushvar.splice($.inArray(removeitem, pushvar), 1);

will remove all the three elements. But i want to remove only 6 from the array when i click on remove class. How can i do this.

4

5 Answers 5

1

Try this:

$(".remove", document.getElementById("TXT")).live("click", function () {
    $(this).parent().remove();
    var removeitem = $(this).parent().attr('id');

    if ($.inArray(removeitem, pushvar) > 0) pushvar.splice($.inArray(removeitem, pushvar), 1);
});
Sign up to request clarification or add additional context in comments.

6 Comments

@Naidu: try this. Your code will work fine now, even if the function runs for 3 times or more...
@william actuall the function runs for two times and remove two elements every time. But i want to remove only one element on single click
it is not entering the if condition
@Naidu Because you use only number for id and $.inArray returns -1 so you convert it as integer as $.inArray(parseInt(removeitem),pushvar)
Yah you are correct but sometimes it is going inside the if and some times it is not
|
1

You can do something like this:

var myArray = [10, 20, 30, 40, 50];
var removeThis = 30;

myArray = jQuery.grep(myArray, function(data){
return data != removeThis;
});

Comments

1

I'd do something like this:

$("#TXT").on("click", ".remove", function () {      
    var removeitem = $(this).parent().remove().attr('id');     
    pushvar.splice($.inArray(removeitem, pushvar)-1, 1);
});

As that seems to work for me -> FIDDLE , but without any HTML there's a lot of guessing ?

Comments

1

Try like this

pushvar = [5,6,7];

$('.remove').on("click", function () {    
      var removeitem = $(this).attr('id');  
      pushvar.splice($.inArray(parseInt(removeitem), pushvar), 1);
      document.write(pushvar);
      return false;
  });

See Demo

Comments

1

live() is deprecated

jQueryversion deprecated: 1.7, removed: 1.9

so use on

 $(".remove,#TXT").on("click", function () {      
      var removeitem = $(this).parent().attr('id');  //get parent id                        
      $(this).parent().remove(); //remove later
      pushvar.splice($.inArray(removeitem, pushvar), 1);

  });

if in case your selector is added dynamically then use on delegate

  $(document).on("click", ".remove,#TXT",  function () {  

4 Comments

The .on function will repeat only for one time isn't it?
no... it just adds the click event to the selector....and have nothing to do with repeat only one time. the above code will run as many time as user clicked the selector
I mean to say if the user click the remove class for the first time it will repeat only time and so on. Not like repeating 3 time on every remove class click
@Naidu: you're confusing on with one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.