Lee
Lee

Reputation: 5936

Binding events for ajax calls - how do you do it?

I have a "what's the best method" type of question, as i believe my current way of working is somewhat ambiguous ..

Starting off with a simple page with a few menu buttons.. Each menu has a single event bound to it, in order to populate the main area in the following manner..

$('#admin_panel_inner>a').bind('click', function()
{
   $.get(url, function(data)
   {
      $('#admin_panel_wrapper').html(data);
   });
});

So far so good, but now my question really starts here.. I could ajax in any number of html templates with the method above, each containing a table, an edit button or a create button for example..

The next step I see however would be to bind the above buttons/links brought in by the ajax to another set of event handlers to catch their click events.. I see this quickly getting out of control with many events bound that may not even exist..

If i require the layout manager page, how do i trigger the event listeners for JUST the layout and not for all of the other "could be" ajaxed html templates..

I hope that is a little clearer.. Thanks again,

Upvotes: 1

Views: 171

Answers (1)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

Usually the best method is the least obtrusive.

Usually I make an init() function that is called when the DOM is ready. It hooks up all the event handlers based on ID and class name. I avoid inline "onclick" as much as possible.

Upvotes: 1

Related Questions