Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Elements From The DOM And Add Them Back In Where They Were

I've got a modal window. What I want to happen is to remove certain elements from the page when the modal opens and add them back in right where they were after the modal closes. I don't want to do display:none, because that only hides them, I need them to actually be removed from the page. So I have a bit of jQuery to remove and add them back in after a timer just for testing...

UPDATED: With these additions to the code, it now grabs the element before, then adds it back in after that same element. The issue is, what if that element was also removed? Then it won't add back in! Also, won't javascript event handlers be lost in this? I'm developign a plugin, so it should interfere with the site as little as possibl,e but 3d elements have a bug in them with Safari that is impossible to get around.

Any ideas on how I could temporarily remove 3d elements without interfering with people's site too much?

$3delements = $('*').filter(function(){return $(this).css('-webkit-transform-style') == 'preserve-3d'});
$3delementsposition = $3delements.prev()

//On modal open
$3delements.remove();

//On modal close
$3delementsposition.after($3delements);

The problem is that this requires I specify a certain place in the DOM for them to come back in. I'd like the elements to come back in where they were. How can I make sure the elements don't change/move/lost information on the .remove to the .append.

like image 443
alt Avatar asked Apr 19 '12 05:04

alt


People also ask

How do I add and remove elements from a DOM?

Removing an element using the removeChild() method To remove an element from the DOM, you follow these steps: First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.

How do you remove an element from the DOM?

The Element. remove() method removes the element from the DOM.

How do you hide element in DOM and still be able to add it back later what should you do?

If we'd like to place the element back, all we need to do is to set its visibility property to nothing.

How do you add and remove an element?

To add an HTML element in your JavaScript program, you can use the appendChild() and insertBefore() method, whereas, for deleting any specific HTML element or a child HTML node, remove() and removeChild() methods are used according to your requirements.


1 Answers

  1. Use .detach() and .append() to remove and reattach elements, it will maintain all your events and data.

  2. If you add elements back in the reverse order that you removed them, they should all fall back in place


untested code


var elems3d = $(...);
var elemsRemoved = [];

// removing
elems3d.each(function(i,o) {
   var elem = $(o);
   elemsRemoved.push({
       loc: elem.prev(),
       obj: elem.detach()
   });
});

// adding back
while (elemsRemoved.length) {
   var elem = elemsRemoved.pop();
   elem.loc.after(elem.obj);
}
like image 125
rkw Avatar answered Sep 28 '22 10:09

rkw