views:

264

answers:

3

This may seem like an obvious thing, but I can't find it. When using resizable, I want to save the new width of the image, but how do I access the ID attribute of the image that I have just resized? This is my code:

$('img.article_image').resizable({
    aspectRatio: true,
    handles: "se",
    stop: function(event, ui){
     // Here I want to access the ID attribute
     // of the img.article_image I am resizing
}});

So, the ui object has ui.helper, which I can't use. I.e. ui.helper.attr("id") or $(ui.helper).attr("id") are both undefined. I can't use $(this).attr("id") either, but I CAN use $(this).width() to see the new width, so it's very odd.

What am I doing wrong? When using draggable() I seem to be able to access $(this) the correct way, but not with resizable... Any suggestions?

A: 

In your case you could just get the ID inside of stop callback with this code:

$('img.article_image').attr('id');

This will however duplicate the selector but parameters passed by stop callback function doesn't have the record of the original object that was resized.

RaYell
This will not work if there are multiple images with the `.article_image` class
joshperry
A: 

I haven't used the resizeable plugin but if it follows the same guidelines as the built-in jQuery events then this will be a reference to the affected DOM element. So you can get a jQuery wrapped object like so:

$('img.article_image').resizable({
    aspectRatio: true,
    handles: "se",
    stop: function(event, ui){
        var img = $(this);
        var id = img.attr("id");
}});
joshperry
+1  A: 

the ui parameter holds the resized element

stop: function(event, ui){
 alert(ui.originalElement[0].id);
}
jitter
This was (semi-)correct, this will fetch the correct ID:<code>$(ui.originalElement[0]).attr("id")</code>
Sandman
If this is meant to be used then someone should add this to the documentation at the jQuery UI site. I'm not sure if I would feel comfortable using an undocumented property as it could go away in a future version
joshperry
@Sandman - actually just `ui.originalElement.attr("id")` would suffice. The `ui.originalElement[0]` notation is used to pull a DOM object out of it's jQuery wrapper.
joshperry