Setting z-index on draggable elements Setting z-index on draggable elements jquery jquery

Setting z-index on draggable elements


All you need to do is use draggable({stack: "div"})Now when you drag a div it will automatically come to the top.

Check working example at http://jsfiddle.net/LQ4JT/8/


I have updated your CSS and Javascript. Don't use "!important" in css unless you are that much desperate.

http://jsfiddle.net/LQ4JT/7/

    $(document).ready(function() {        var a = 3;        $('#box1,#box2,#box3,#box4').draggable({            start: function(event, ui) { $(this).css("z-index", a++); }        });    $('#dragZone div').click(function() {        $(this).addClass('top').removeClass('bottom');        $(this).siblings().removeClass('top').addClass('bottom');        $(this).css("z-index", a++);    });

});​

Though this answer works it has the limitation of max number of 2^31−1 in javascript.refer What is JavaScript's highest integer value that a Number can go to without losing precision? for more info.


The easiest way I found to solve this problem was to use the appendTo and zIndex options.

$('#box1,#box2,#box3,#box4').draggable({  appendTo: "body",  zIndex: 10000});