Pergunta

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>
Foi útil?

Solução

$('#myButton').live('click', function(){
      //Do something
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top