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 kno
You can use JQuery's built in .index()
method to get the location of a child element and get the .length
value form a parent element to find out the number of elements.
If index
comes back 0, it is the first element, if the index
comes back parent element length -1 then you know it is the last element.
For moving the elements them self you can use .insertBefore
and .prev
to move an element up.
var elm = $("selector_for_the_element");
elm.insertBefore(elm.prev());
For moving an elemetn down then you can use insertAfter
and .next
var elm = $("selector_for_the_element");
elm.insertAfter(elm.next());