DOM - Move element?
I am creating a web app and I want to be able to move an object to another spot.
Right now what I have done is copy the html of the old one, insert it to the end of the node I want, then delete the old one.
As a result, I lose events/observers that have been applied (i.e. for double clicking, dragging, etc.)
Edit: I also have <input> boxes in the element I wanna copy, so if I just get the HTML I lose their values too. If there is some way to copy the whole object, with the "correct" values, applied events, etc.. that would be WONDERFUL!
removeChild() return a reference to the node you removed which you can then use insertBefore() or appendChild() to make it be a child of another element.
Originally Posted by Kravvitz
thank you so much
There is no need to copy and remove, simply have a reference to the node and use appendChild method. It will then move it.
triexa, you're welcome :)
Pepejeria, you're right, the specs says that the call to removeChild() isn't necessary. :) Do you know if there are any browsers that don't implement that correctly?
So what would be the method to do it? And as asked, are there any browsers that you are aware of that do not correctly implement this?
Originally Posted by Pepejeria
This works as expected in all browsers that I tested (IE, Firefox, Opera, Safari).
triexa, simply access the element with getElementById, and then use the appendChild method on the node you want to add it to. It will then move it.