Jquery: rewrite chained code using live

hi guys, i wish the jquery community are looking here right now.

i have this existing code, which looks like this

$('selector').mousedown(function() {
	do something here
	do something again
	another code here
}).find('.window').dblclick(function() {
	yet another code
}).find('img').dblclick(function() {				
	set another set of parameters here

This code is called once on DOM ready. but when i started inserting new elements, i figured i needed to use event delegation so i wouldn’t need to run this code again. And here is where i fail, i couldn’t maintain the “chaining” when using .live(). this is as far as i could go

$('div.window').live('mousedown', function() {
	do something here

from there, with my limited experience, i failed to maintain the chaining. where do i chain the draggable (and the rest of the lot) event? :frowning:

The jQuery live() documentation has a multiple events section that reveals all.

hi pmw57, actually ive read that link before i started coding, but it didnt really help. see, the draggable() and resizable() cant be used on live(), or thats what i think, cause they are not bound to particular events.

Everytime I insert a new element I have to add the draggable() and resizable() functions. what im trying to accomplish is to eliminate that part by using event delegation. so far i cant make use of live(), else what will happen is that everytime i mousedown on a div, the dragabble() and resizable() will always fire, which doesnt need to.

so basically, i need my old code recoded such that it will support event delegation of the draggable() and resizable() in particular. Those two are from the jQuery UI library BTW.

Did you know that you can use custom events too, and trigger that custom event when you create the elements?

no i dont, sorry but javascript is unchartered territory to me. i could only do so much editing on an existing code. I’ll try to read about custom event, but if you could give me a sample, i would really appreciate it.

Sure, it’s from the same page as before jQuery.live() custom event example

ok, now im able to make use of custom events. i create a custom event which live will listen to, or sort of. then i make use of trigger to call it. now my question is, what is the advantage of using live+custom event+trigger against just using a function and calling it just like trigger? for chaining? i was thinking earlier while coding, why should i put it in a custom event, when i could simple create a function and call it?

Because it provides a controlled environment that can be more easily controlled and maintained than by having a random function floating around. And it can be chained :slight_smile:

If it can be chained, then its important! thanks man, im able to move on on my project now. appreciate it :smiley: