0

I want to get the id of the div i am moving. I have tried different methods, nothing worked. So why isn't this working?

$('#content').children().draggable({
drag: function (event, ui) {
    var id = $(this).attr("id");
    $('#textarea').val(id);
}

http://jsfiddle.net/pX2T4/

Koiski
  • 568
  • 2
  • 8
  • 23
  • 2
    [The code is fine. **Your draggable div does not have an ID.**](http://jsfiddle.net/mattball/JfRPX/) And just an FYI, `this.id` can always be used instead of `$(this).attr('id')`. http://stackoverflow.com/q/4651923/139010 – Matt Ball Mar 17 '13 at 16:18

3 Answers3

2

This doesn't work because you are not finding the id of $('#content'), but of the child div, which does not have an id set. Thus, give the child an ID and it will find that one.

Brad M
  • 7,857
  • 1
  • 23
  • 40
1

It's because $(this) is refering to the inner div marked with the class

see http://jsfiddle.net/pX2T4/

<div id="content">
    <div class="content" id="foo">
        <textarea name="" id="textarea" cols="30" rows="10"></textarea>
    </div>
</div>

$('#content').children().draggable({
    drag: function (event, ui) {
        var id = $(this).attr("id");
        console.log(id);
        console.log($(this));
        $('#textarea').val(id);
    }
});

outputs "foo"

magritte
  • 7,396
  • 10
  • 59
  • 79
0

Instead $(this) use $("#content") Try this:

http://jsfiddle.net/pX2T4/4/

KF2
  • 9,887
  • 8
  • 44
  • 77