开发者

How to get which div/ui.helper clicked in drag event jqueryui/jquery?

开发者 https://www.devze.com 2023-03-02 07:56 出处:网络
On Jquery UI\'s site: http://jqueryui.com/demos/draggable/ If I have: <div id=\"someId\"开发者_StackOverflow社区 class=\"someClass\">he</div>

On Jquery UI's site:

http://jqueryui.com/demos/draggable/

If I have:

<div id="someId"开发者_StackOverflow社区 class="someClass">he</div>
<div id="otherId" class="otherClass">he2</div>

And:

$('#someid','#otherid').draggable({
    drag: function(event, ui) {
      alert(ui.helper.THEIDOFTHECLICKEDITEM); // What goes here?
    }
});

How do I get the id or class of the ID using the "ui" variable from the callback? If not possible, how do I get it from the "event" variable?


You want:

$("#someId, #otherId").draggable({
    drag: function(event, ui) {
        console.log(ui.helper[0].id);
    }
});

(or use ui.helper.attr("id"))

Note: ui.helper is a jQuery object, which is why we must either use .attr("...") to retrieve the id or access the matched element at index 0 and directly get the id.


Or without using the ui argument (probably what I'd recommend):

$("#someId, #otherId").draggable({
    drag: function(event, ui) {
        console.log(this.id); // "this" is the DOM element being dragged.
    }
});

Here's a working example: http://jsfiddle.net/andrewwhitaker/LkcSx/

0

精彩评论

暂无评论...
验证码 换一张
取 消