I’m a little lost on moving a div within the dom. I basically need to change the order of some divs. Should I remove it and then reinsert it somehow or is there a better way to just move it in the dom tree.
Thanks for any insight.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you only need to move it, and don’t need to make any reflow-triggering changes to the element with JavaScript, simply use
appendChild().On the other hand, if you need to make changes to the element, update its properties or ‘physical’ dimensions that would trigger a document reflow, then it’s worth removing it from the DOM making the changes and then reinserting with either
appendChild()orinsertBefore().Bear in mind there’s already going to be one document reflow, simply by relocating the element, but if any other changes are to be made then try to do them ‘behind the scenes’ with the element/node as a documentFragment before reinserting.
References:
appendChild().insertBefore().