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

Ricardo
  • 3,696
  • 5
  • 36
  • 50

2 Answers2

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();
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

Using the context argument of the jQuery() function:

$('div', div).remove('#div1');
Ricardo
  • 3,696
  • 5
  • 36
  • 50