1

I want to draw divs to a specific position using jQuery UI's selectable,

I've tried it and I thought it's almost figured out: http://jsfiddle.net/62LJG/

The steps I've thought is:

  1. Fetch Selectable's helper's left and top.
  2. Fetch Selectable's helper's width and height.
  3. Append a div to my target and I can use these left, top, width, height I've got to CSS this div.

Now my problem is I can't get the width and height from this widget.

I thought all the jQuery UI are similar so I tried this:

$('#tar').selectable({
  stop: function(event, ui) {
    console.log(ui.size.width + ',' + ui.size.height);
  }
});

but this part ui.size doesn't work.

But I can use ui.size in jQuery resizable or draggable to get width and height.

How can I get width and height from jQuery selectable's helper(dotted line) to achieve my goal?

Baozi Wu
  • 53
  • 1
  • 5

2 Answers2

0

The ui object seems to be empty, use this:

$(function() {
    $('#tar').selectable({
      stop: function(event, ui) {
        console.log($(this).width() + ',' + $(this).height());
      }
    });
});

If you have padding/margin in your element use $(this).outerWidth() and $(this).outerHeight().

Wilmer
  • 448
  • 2
  • 4
0

Finally I figure it out.

event.page - position() will get the relative value.

demo: http://jsfiddle.net/62LJG/1/

reference: Using jQuery how to get click coordinates on the target element

Community
  • 1
  • 1
Baozi Wu
  • 53
  • 1
  • 5