views:

43

answers:

1

Hi all,

I've been using the jQuery templates code (jquery.tmpl.js on github) and it's been working very well for me. However, I'm trying to attach events to the content using delegate and it's not working. Here is my simplified HTML structure after the templates are inserted:

<!-- static in the html file -->
<div id="container">
  <!-- this div is inserted multiple times by the jquery template code -->
  <div class="rule">
    <button>Save</button>
  </div>
</div>

In my Javascript I have this set up

$(".rule").live("click", function() {console.log("live click");});

$("#container").delegate("click", ".rule button", function() {
  console.log("delegate click");
});

When the page runs I can click on the buttons inserted by the template, but I only get "live click" messages in the console log of Firebug. The delegate events never fire. I've tried several different targets/context for delegate, though I think what I have above is correct, but I can't get it to work.

Anyone have any suggestions, pointers or things I could try?

Thanks! Doug

+3  A: 

the correct syntax for delegate() is delegate( selector, eventType, handler )

use

$("#container").delegate(".rule button", "click", function() {console.log("delegate click");});
GerManson
The `<button>` doesn't have a `class="rule"`, it's *within* a `<div class="rule">`
Nick Craver
sorry man, I edited my answer
GerManson
Thanks for the "slap upside the head", that cleared things up. I swear I've seen documentation for delegate that says "delegate(eventType, selector, handler)", but what you suggested worked well. I think part of my problem, besides the incorrectly formatted delegate call, was the context selector I've been using. Thanks for your help!
writes_on
can you mark my answer to accepted?
GerManson