Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling jQuery onClick event on handlebars

I would like to set up a simple jQuery onClick event to make the UI dynamic on a handlebars template. I was wondering to addClass() after a specific click.

consider the HTML (generated by handlebars)

  {{#if hasButton}}
      <div id="container">      
          <button type="submit" class="myButton">Click me!</button>
      </div>
  {{/if}}

i.e: After a click within a button, its container will receive a loading class that will create the interaction using CSS.

$(".myButton").on("click", function(event){
        $(this).parent().addClass("loading");
});

This code should goes on my handlebars-template or should I rewrite it into a specific helper for it? Is there any examples that could be provided so I can study it then develop something similar?

Thanks in advance!

like image 286
Hal Avatar asked Jul 09 '13 12:07

Hal


1 Answers

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! :-)

like image 189
murnax Avatar answered Sep 25 '22 06:09

murnax