Event handlers for Underscore.js generated templates
-
28-10-2019 - |
Question
I have my template in a separate file from the rest of the js code. I'm trying to do eventhandlers for my template (for example my onclick event below), but it doesn't work. Can anybody tell me how I can solve this?
var output = _.template($('#myTemplate').html(), {myData});
$('#content').html(output);
//This doesn't work
$('#myButton').click(function(){
//Do something
});
//My template
<script type="text/template" id="myTemplate">
<h2><%= myTitle %></h2>
<button id="myButton">Button</button>
</script>
Solution
$('#myButton').live('click', function(){
//Do something
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow