Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move containing elements up and down with jquery

Thanks for any help you can provide! Currently, I use a ui-sortable code to allow users to move items up and down in order. Now, I want to give each of these items a set of "up" and "down" buttons that allow users to move items up and down with a click. I've tried reworking theses posts, but I seem to be missing something obvious...

jQuery Sortable Move UP/DOWN Button move up/down in jquery

I think somehow I'm not applying the jquery to the right element. My jsfiddle is here: http://jsfiddle.net/kevindp78/vexw5/2/ and code is below.

HTML

<div id="box" class="ui-sortable" style="display: block;"> 
<div class="leg">
    <a class="image">IMG1</a>
    <div class="details">
        <h3><a href="/details">Info</a></h3>
        <span class="moreinfo">MoreInfo</span>
    </div>
    <div class="clear"></div>
    <div class="up-down">
        <p>Change Order</p>
        <div class="up">
            <input type="button" value="Up" class="up-button"/>
        </div>
        <div class="down">
            <input type="button" value="down" class="down-button"/>
        </div>
    </div>
    <div class="morestuff">
        Stuff1
    </div>
</div>
<div class="leg">
    <a class="image">IMG2</a>
    <div class="details">
        <h3><a href="/details">Info</a></h3>
        <span class="moreinfo">MoreInfo</span>
    </div>
    <div class="clear"></div>
    <div class="up-down">
        <p>Change Order</p>
        <div class="up">
            <input type="button" value="Up" class="up-button"/>
        </div>
        <div class="down">
            <input type="button" value="down" class="down-button"/>
        </div>
    </div>
    <div class="morestuff">
        Stuff2
    </div>
</div>
<div class="leg">
    <a class="image">IMG3</a>
    <div class="details">
        <h3><a href="/details">Info</a></h3>
        <span class="moreinfo">MoreInfo</span>
    </div>
    <div class="clear"></div>
    <div class="up-down">
        <p>Change Order</p>
        <div class="up">
            <input type="button" value="Up" class="up-button"/>
        </div>
        <div class="down">
            <input type="button" value="down" class="down-button"/>
        </div>
    </div>
    <div class="morestuff">
        Stuff3
    </div>

</div>

JS

$('up-button').click(function(){
$(this).parent('.leg').insertBefore.previous('.leg')
});

$('.down-button').click(function(){
$(this).parent('.leg').insertAfter.next('.leg')
});
like image 863
KDP Avatar asked Mar 18 '13 20:03

KDP


People also ask

How to move elements in jQuery?

All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element. jQuery automatically realises that the element(s) to add already exist in the page, and it moves the element(s) to the new parent.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

How do you move an element up in JavaScript?

We can use the element. style property to move the element up, down, right, or left using keyboard arrow keys .

Which jQuery technique do you use to remove the original list?

The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements.


1 Answers

There are several problems to address in your code so let's go through them in order.

First there is $('up-button') it is missing the . so it won't select the buttons.

Next you are using the parent() method which only goes up one level, use parents('.leg') instead.

insertBefore() is a method that accepts a target as to where to place the content you selected.

previous() isn't a function, it is prev() instead and it doesn't need a parameter as it just selects the previous element.

If you combine all of those fixes you would get something like this

$('.up-button').click(function(){
  $(this).parents('.leg').insertBefore($(this).parents('.leg').prev());
});

$('.down-button').click(function(){
  $(this).parents('.leg').insertAfter($(this).parents('.leg').next());
});

As demonstrated on this edited fiddle http://jsfiddle.net/vexw5/6/

like image 108
BeardFist Avatar answered Sep 18 '22 08:09

BeardFist