user1149117
user1149117

Reputation: 131

Event handlers for Underscore.js generated templates

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>

Upvotes: 2

Views: 2120

Answers (1)

Vitalii Petrychuk
Vitalii Petrychuk

Reputation: 14225

$('#myButton').live('click', function(){
      //Do something
});

Upvotes: 2

Related Questions