Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move an element one place up or down in the dom tree with javascript

I want a javascript way to move an element one place up or down in the dom tree within a particular known parent using javascript (or jquery is ok), but i want the script to know when an element is the first or last element within the parent and not be moved. for example, if i have the following ...

<div id='parent_div'>     <div id='div_1'></div>     <div id='div_2'></div>     <div id='div_3'></div> </div> 

on click of a button, i want to pass a known id (let's say div_2) and move it up to the position above it, swapping its position with the element that was previously before it (in this case div_1). The ids of the elements don't have to change, and their new position doesn't need to be known, at least not unless they are moved again.

like image 351
Peter Cullen Avatar asked Jan 21 '16 01:01

Peter Cullen


People also ask

How do you move elements in DOM?

To move element in the DOM in JavaScript, we can use the before and after methods. const div1 = document. getElementById("div1"); const div2 = document. getElementById("div2"); const div3 = document.

How do you get the position of an element in a JavaScript?

Use the Element. getBoundingClientRect() Function to Get the Position of an Element in JavaScript. The getBoundingClientRect() function produces a DOMRect object containing information about an element's size and position in the viewport.


2 Answers

With jQuery:

var e = $("#div_2"); // move up: e.prev().insertAfter(e); // move down: e.next().insertBefore(e); 
like image 79
Gavriel Avatar answered Sep 19 '22 14:09

Gavriel


Move element "up":

if(element.previousElementSibling)   element.parentNode.insertBefore(element, element.previousElementSibling); 

Move element "down":

if(element.nextElementSibling)   element.parentNode.insertBefore(element.nextElementSibling, element); 

function moveUp(element) {    if(element.previousElementSibling)      element.parentNode.insertBefore(element, element.previousElementSibling);  }  function moveDown(element) {    if(element.nextElementSibling)      element.parentNode.insertBefore(element.nextElementSibling, element);  }  document.querySelector('ul').addEventListener('click', function(e) {    if(e.target.className === 'down') moveDown(e.target.parentNode);    else if(e.target.className === 'up') moveUp(e.target.parentNode);  });
.up, .down        { cursor: pointer; }  .up:after         { content: '△'; }  .up:hover:after   { content: '▲'; }  .down:after       { content: '▽'; }  .down:hover:after { content: '▼'; }
<ul>    <li>1<span class="up"></span><span class="down"></span></li>    <li>2<span class="up"></span><span class="down"></span></li>    <li>3<span class="up"></span><span class="down"></span></li>    <li>4<span class="up"></span><span class="down"></span></li>    <li>5<span class="up"></span><span class="down"></span></li>  </ul>
like image 38
Oriol Avatar answered Sep 19 '22 14:09

Oriol