ruby on rails - jQuery .on() not working with jquery-ujs -


so have rails app renders views via jquery-ujs. (data-remote in urls, load of js.erb files things like:

$('#whatever').html('<%= j render("form") %>'); 

in main js file, have event handlers so:

$('.action').on('click', function() {     console.log("clicked"); }); 

but on() behaviour not attach new elements inserted jquery-ujs. can go console , paste above code , attaches fine (once elements there), straight after ujs fires, no luck. bug or doing wrong?

this jquery 1.9.1 , rails 3.2.13 btw.

you need bind on parent document. now, you're binding directly .action element, same using $('.action').click(). because you're adding elements dom later, need delegate handler that's present before elements inserted. can provide selector second parameter of .on event bind .action.

have @ 'direct , delegated events' section in jquery's api documentation .on.

this should work:

$(document).on('click', '.action', function () {     console.log('clicked!'); }); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -