3

I want to use jQuery to manipulate a cloned element that is not on the DOM, to perform actions like .remove() on it. Say I have the following code:

var div= $('<div> <div id="div1"></div> </div>');
div.remove('#div1');
console.log(div.html());

The result on the console will still show that the element was not removed. string manipulation is not desirable, I'm looking for something analogue to $().remove()

2 Answers 2

3

The div variable will contain a reference to the outer div. You need to use find() to get the inner div by its id:

var $div = $('<div><div id="div1"></div></div>');
$div.find('#div1').remove();
Sign up to request clarification or add additional context in comments.

Comments

1

Using the context argument of the jQuery() function:

$('div', div).remove('#div1');

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.