PHP, MYSQL, CSS, LINUX TUTORIALS

How to create a Move Up and Move Down button using jQuery

I’m sorry i haven’t posted new tutorials for a long time.
And very soon i’ll update the wordpress plugin Maribol IMDB.

So what i would like to teach you today is how to move an element using the powerfull jQuery.

Let’s asume that you want to order the order for some text inputs

HTML structure:

View Code HTML
<div>
    <div>
        <input type="text" name="values[]" value="value 1">
        <a href="javascript:;" class="moveUp">&uarr;</a>
        <a href="javascript:;" class="moveDown">&darr;</a>
    </div>
    <div>
        <input type="text" name="values[]" value="value 2">
        <a href="javascript:;" class="moveUp">&uarr;</a>
        <a href="javascript:;" class="moveDown">&darr;</a>
    </div>
    <div>
        <input type="text" name="values[]" value="value 3">
        <a href="javascript:;" class="moveUp">&uarr;</a>
        <a href="javascript:;" class="moveDown">&darr;</a>
    </div>
    <div>
        <input type="text" name="values[]" value="value 4">
        <a href="javascript:;" class="moveUp">&uarr;</a>
        <a href="javascript:;" class="moveDown">&darr;</a>
    </div>
</div>

Javascript:

View Code JAVASCRIPT
    $(document).ready(function() {
        $('.moveUp').click(function(){
            var current = $(this).parent();
            current.prev().before(current);
        });
        $('.moveDown').click(function(){
            var current = $(this).parent();
            current.next().after(current);
        });
    });

View demo here

If you have any question please leave them in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>