views:

406

answers:

1

I want to add a div on a click and automatically start the resize mouse ui functionality with the mousedown.

I can add the div easily, I have the resize functionality easy. But I can't figure out how to pass the mouse event and bind them so that the resize can start right away.

Imagine a painting like program so the div can be added and drawn by dragging the mouse...?

Thank you so much.

+2  A: 

Because your divs are added to the DOM AFTER the initial event handlers are bound, .bind() won't work on the new elements. jQuery has a nifty .live() method which will do the same thing as .bind(), but to elements added to the DOM later on.

So, you could write something roughly like this:

$('.my_new_div').live('mousedown', my_resize_handler);

jQuery Live

Edit: also look at the new .delegate() method. Very similar to .live() but more efficient.

rbaker86
thanks, that's a great tip. wish I'd known about "live" a few weeks back
Chris Simpson
.live() or .delegate() are pefectly suited to rich UIs where new content and elements are created on the fly. It saves having to explicity re-bind everything in order to attach an event hander.
rbaker86
That is pretty interesting; however, I want to fire an event on an item that does seem live. If I click on the item on the corners to resize it works just fine. I want the already active mousedown to be the mousedown for the resize as if I had clicked on the corners.I do appreciate the help so far though.
msj121