Tom el Safadi
Tom el Safadi

Reputation: 6776

Remove OBJECT with jQuery

How can I remove an object with jquery.

$('.Line1').each(function (i, obj) {
      if (obj.id != myVariable) {

      }

});

See that I can't remove this object with $( ".hello" ).remove(); because it is an object. How can I do it with the code from above?

Thanks

Upvotes: 1

Views: 49

Answers (2)

Tsafadi
Tsafadi

Reputation: 28

Do it like this:

$('.Line1').each(function (i, obj) {
      if (obj.id != myVariable) {
          $(obj).remove();
      } 
});

When you loop through a jQuery object using each, you get the elements themselves, not each element wrapped in a jQuery object. You need to wrap each element in a jQuery object to use the remove method:

Upvotes: 1

MortenMoulder
MortenMoulder

Reputation: 6646

Inside your .each() you can grab the current element with $(this). Then you can do something like:

$(".elements").each(function() {
    if("some-statement" == "true") {
        $(this).remove();
    }
});

This will remove it from the DOM. Alternatively you can hide it using .hide().

Upvotes: 1

Related Questions