1

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

3
  • Did you try making object null explicitly? obj=null Commented May 2, 2017 at 23:52
  • What do you mean by remove? Are you asking to remove a property from an object? Which object? Usually in JS, you can use delete obj.id to delete the id property from an obj. Commented May 2, 2017 at 23:52
  • I want to delete it but dw I found it out. It is $(obj).remove(); Commented May 2, 2017 at 23:53

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

Comments

1

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().

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.