views:

208

answers:

3

So today I just came across the 'live()' function that binds any future and past elements to the whatever event you choose, such as 'onclick'.

Right now I'm having to set up buttons like the following each time I load a new button via ajax ...

$('a.btn.plus').button({icons:{primary:'ui-icon-plusthick'}});
$('a.btn.pencil').button({icons:{primary:'ui-icon ui-icon-pencil'}});
$('a.btn.bigx').button({icons:{primary:'ui-icon ui-icon-closethick'}});

So, instead of calling these lines each time I use ajax to add a new button, is there a similar way to tell JQuery to setup my buttons ANYTIME I add new ones?

Thanks for any help!

A: 

Mmh not really. But there is the function .ajaxSuccess() which is triggered whenever an Ajax call is successful. So you could do:

$('body').ajaxSuccess(function() {
    $('a.btn.plus').button({icons:{primary:'ui-icon-plusthick'}});
    $('a.btn.pencil').button({icons:{primary:'ui-icon ui-icon-pencil'}});
    $('a.btn.bigx').button({icons:{primary:'ui-icon ui-icon-closethick'}});
});

But this will run on any links with the classes, not only on the new ones. But if you append them on a time (i.e. not multiple a.btn.plus at once) you might be able to use the :last selector (a.btn.plus:last).


You can also create a function and just that from your callback functions:

function links() {
   $('a.btn.plus').button({icons:{primary:'ui-icon-plusthick'}});
   $('a.btn.pencil').button({icons:{primary:'ui-icon ui-icon-pencil'}});
   $('a.btn.bigx').button({icons:{primary:'ui-icon ui-icon-closethick'}});
}

and in the Ajax call:

$.ajax({
   //...
   success: function(msg){
      links();
   }
});

This way you can pass the parent element to the function in order to find the link only inside this element (so the code would only work on the new links).


A last option would be generate a custom event but in the end this would be similar to just doing a function call in your case so you gain not much.

Felix Kling
hey cat, thanks for the advice! Using the ajax success to update the buttons looks like my best option right now.
Dan
A: 

You can use delegate in your success function too

$("body").delegate("a.btn", "hover", function(){
$(this).toggleClass("hover");
});
XGreen
Somehow I cannot associate your answer with the OP's question. How is this related to generating a button?
Felix Kling
"delegate" description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.Not really sure how to use it yet, but the word 'future' makes me want to try some experients in the hopes that it really does what I think it does.
Dan
actually, i think this is exactly what I was looking for..."Delegate is an alternative to using the .live() method, allowing for each binding of event delegation to specific DOM elements."
Dan
@Dan: Yes but I thought that you don't want to bind an event to the links, you want to perform different actions on the links. `delegate` is similar to `live` you can bind events to elements that don't exist yet.
Felix Kling
@Dan: But you cannot perform actions on new elements when the come to existence with `live` or `delegate`. Just bind events.
Felix Kling
crap you're right, just ran some tests, it has to have an EVENT. Back to using the ajax success function
Dan
A: 

There is a Jquery Plugin called livequery which covers your requirements.

I like to think of this plugin as Jquery .live() but without the need for an event ('click') etc.

You can find more info here//

Jquery - Live Query Plugin

Blowsie