0

I have a Rails Page View that is constructed like this:

script>

$('#carnum_driver_plus').click(function () {
    alert("click done");
});

</script>

....
<%= form_tag(.....(method: :post)) do %>

  <button id="carnum_driver_plus" type="button">
    <span class="glyphicon glyphicon-plus align-text-bottom" aria-hidden="true"></span>
  </button>

<% end %>

When I load this page the first time, the click handler worked and called the alert function. When I submit the form, it will be processed by a controller action, and then the same page view will be rendered. But then when I click the button again, nothing happened, the alert will not be called.

Any ideas?

Sven Kirsten
  • 478
  • 1
  • 8
  • 27

2 Answers2

2
<script>

$(document).on('turbolinks:load', function() {
  $('#carnum_driver_plus').click(function () {
    alert("click done");
  });
});

</script>
Timmy Von Heiss
  • 2,160
  • 17
  • 39
0

Try switching .click to .on()

$('#carnum_driver_plus').on('click', function() { 
//event handler...
}

Check out this question here.

Foxhound013
  • 301
  • 3
  • 13