Grégoire Borel
Grégoire Borel

Reputation: 2026

CoffeeScript function not triggered

I have several inputs generated by a loop inside a show page. They all have the same name. When of of them is unfocused, I'd like to call a JS script. However the signal is never triggered. Why?

expenses.js.coffee

alert 'Test' // This instruction is detected

$("input[value='nb_kms[]']").blur -> // This one is not
   alert 'Handler for .blur() called.'
return

show.html.erb

<% @calendar.each_with_index do |d, i| %>       
  <tr>
    <td><input type="number" name="nb_kms[]" value="<%= @nb_kms[i] %>"></td>
  </tr>

Upvotes: 0

Views: 95

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

Did you try this?

$("body").on "blur", "input[value='nb_kms[]']", ()->

or do this, add class to the input

<tr>
  <td><input type="number" name="nb_kms[]" value="<%= @nb_kms[i] %>" class="kms_input"></td>
</tr>

in js

ready = ()->
  $("body").on "blur", ".kms_input", ()->
    alert $(this).val()
    alert 'Handler for .blur() called.'

$(document).ready ready
$(document).on 'page:load', ready

Upvotes: 2

Related Questions