handling jQuery onClick event on handlebars

痴心易碎 提交于 2019-11-30 11:36:46

You have to refresh your Jquery listeners AFTER the insertion of nodes into your DOM HTML.

var source   = "<li><a href="{{uri}}">{{label}}</a></li>";
var template = Handlebars.compile(source);
var context = {"uri":"http://example.com", "label":"my label"};
$("ul").append( template(context) );

// add your JQuery event listeners
$("li").click(function(){ alert("success"); });
murnax

there is no need to reattach the event handler on every dynamic DOM update if you're defining them at document level:

$(document).on('click','li',function(){
   alert( 'success' );
});

Hope this helps! :-)

I am not sure what your problem exactly is. It's correct like this if you keep your JavaScript in a *.js file, perhaps using parent() instead on prev() in this specific case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!