tags:

views:

77

answers:

2

I am using this function for drag n drop. Calling this function onmousedown event. It does not work when i did mouse down for the first time but it works perfectly fine from the second mouse down event. Can anyone tell me how can i make it work for the first time??

function DivMouseDown(id) { alert("id:" +id); //alert(" i m in DivMouseDown"); try {

jQuery('#'+ id).draggable({ opacity: 0.5, revert: 'invalid', drag:function(ev,ui) { //alert("i m draggable"); } }); }

catch (e) {

alert ("exception in DIVmouse down: "+ e); }

+1  A: 

You should call that init function once the DOM is ready.

function initDivMouseDown(id)
{ 
    try {
        jQuery('#'+ id).draggable({ 
            opacity: 0.5, 
            revert: 'invalid', 
            drag:function(ev,ui) {
                // ADDED DIV ID HERE 
                alert($(this).attr('id') + " is being dragged");
            } 
        }); 
    } catch (e) {
        alert ("exception in DIVmouse down: "+ e); 
    }
}

jQuery(document).ready(function() {
    initDivMouseDown("my_div");
});
Makram Saleh
A: 

I have made an html page page over which i have created a div whenever i do mouse down i pass the its current div id How can i send in the id as it changes on calling it on different divs

Check my edited answer again. I added alert($(this).attr('id') + " is being dragged");
Makram Saleh