Search code examples
javascriptjqueryhtmldom-manipulation

Manipulating jQuery object without changing the html element


I have a variable with a jQuery object, for example:

var div = $('#div_element');

How can I manipulate ONLY the div variable, without changing the #div_element itself?


I want to do some edits on the div variable and to pass it as an argument to a plugin, like this:

var el = $('#div');
el.find(':first').remove();
$().popup(el); //I wrote this plugin myself

Actually I want to display popup containing the #div element (with removed the first "child"), but don't want to change the #div element itself.


Solution

  • Use clone to create a copy of the element.

    var el = $('#div').clone();