Search code examples
javascriptjqueryjtemplates

Cannot hook Click Event from a jTemplate output


When using a foreach from jTemplate, jQuery doesn't hook into the .click() event of any DOM Elements within the applied template.

Solutions such as .live('click', function()) do not work (Partially because .live was deprecated, but the alternative introduced to replace it .on('click', function()) does the same thing...), and i cannot figure out how to get Click events from the results of a jTemplate.

Thing's i've tried:

  • .live('click', function())
  • .click(function())
  • .on('click', function())
  • All 3 events in $(window).load(function()), $(document).ready(function()), and just freely in a Script tag
  • All 3 events before and after calling the .processTemplate() function

At this point, i have no more ideas on how to solve this issue.

My jQuery version for reference is 2.1.3, and jTemplate is 0.8.4.


Solution

  • As for jQuery 1.7 and above, to delegate event, you should use on() method and passing selector parameter.

    Here is an example, even it is usally better to bind it to closest static container, not the document:

    $(document).on('click', 'selector', handler);
    

    You can as a good read see following link: http://learn.jquery.com/events/event-delegation/