Search code examples
jqueryruby-on-railsturbolinks

on click function does not work on elements appended with ajax


 $(document).on('turbolinks:load', function() {
    $('.box').on("click", function() {
      alert('hello')
    });
  });

This does not work on elements with class box that are appended with ajax. It only works on the elements that are present on the initial loading of the page. I have also tried as wrappers:

$(document).ready(function() { }); 

and

var helloscript = function() { };

but nothing works.


Solution

  • You may call it like:

    $(document).on('click', '.box', function (event) {
    
    });