tags:

views:

47

answers:

3

i have a list of images i need to be able to move them left and right (sortable) in jquery. I know I need to use Child Selector but can someone lead me down the right path to getting this working please.

e.g so say i click id=1 and i want to move it below id="3"

    <div id="image_list">
    <img src="test0.png" id="1"></img>
    <img src="test1.png" id="2"></img>
    <img src="test2.png" id="3"></img>
    <img src="test3.png" id="4"></img>
    </div>

<div id="move_opt">
<div id="left">Move Image Left</div>
<div id="right">Move Image Right</div>
</div>


$('#image_list').live('click', function() {
    var img_class = $(this).attr("class"); 
    var img_src = this.src;
     $('#img_prop').css("display","block");
     $('#pre_img').html("<img src='"+img_src+"'></img>");
}); 

Thank you

A: 

are you wanting to implement drag and drop? see the jQuery UI Draggable and Sortable documentaion.

or a clickable scroller? See an example one that I made here: http://jsbin.com/abape3

Moin Zaman
i dont what it so all the image slide. I am trying to get it so you can order them.
Gully
So you want to select and `image` first, and then move the selected `image` left or right by clicking the move left / right `divs`?
Moin Zaman
yes that right would you be able to help me please
Gully
see my next answer
Moin Zaman
+1  A: 
$(function(){
    var movingImage = null;
    $('#image_list img').click(function(event) {
        movingImage = event.target;
    });

    // DOM ensures an element only exists once in a document, so
    // you can just insert it before or after the next or previous
    // image.
    $('#left').click(​function(event){
        if (movingImage) {
            $(movingImage).insertBefore($(movingImage).prev('img'));
        }
     });
    $('#right').click(function(event){
        if (movingImage) {
            $(movingImage).insertAfter($(movingImage).next('img'));
        }
    });
});

Here's a demo:

http://jsfiddle.net/nickh/Bv4xX/

Nick
I have tryed it with the click i make and it does not work i have put the code in the question.
Gully
Sorry, I don't understand what you're trying to do. If you want to support adding more images later on, you can change my line 3 to: `$('#image_list img').live('click', function(event) {`
Nick
A: 

See this example. you can change the input elements to images or any other elements.

http://jsbin.com/abape3/10/

Moin Zaman