I have a form with 3 input fields, strokes, putts, and GIR:
<%= form_for(@played_hole) do |f| %>
<%= f.hidden_field :hole_par, value: @hole.par %>
<%= f.label :strokes, "How many strokes?" %>
<%= f.select(:strokes, options_for_select(generate_strokes_array(@hole.par), selected: @hole.par)) %>
<%= f.label :putts, "How many putts?" %>
<%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: calculateGIR() %>
<%= f.label :GIR, "Green in Regulation?" %>
<%= f.select(:GIR, [['Yes', 1], ['No', 0]]) %>
<%= f.submit "Next Hole", class: "btn btn-large btn-success" %>
<% end %>
My goal is to automatically select the 3rd field (:GIR) once the user clicks on the second input, putts. For example, if the hole is a par 4 and they selected 5 in the :strokes field and 2 in the :putts field, then I would know that they didn't get a Green In Regulation. So, I'd like to automatically switch the :GIR select to 'No'.
I'm currently having trouble just getting an onclick() event registered. I've added onclick: calculateGIR() to the :putts select field but I'm getting this error in my PlayedHole#new view: undefined method calculateGIR' for #<#<Class:0xb5a684c4>:0xb591eac8>
I've added this to the javascript/played_holes.js file: function calculateGIR() { alert("calculating GIR"); } but obviously it's not being picked up.
Basically, I could use some help calling javascript (or jQuery) functions within a view. And then once I get that setup I'd like to figure out how to pull the selected value from :strokes and :putts to calculate the GIR.
For those curious, a GIR means you were on the green in 2 (or less) strokes than the par for the hole. i.e. Par 3 - 1 stroke, Par 4 - 2 strokes, Par 5 - 3 (or less) strokes.
And I'm running Rails 3.2, in case you're curious.
Thanks!
<%= f.select :putts, options_for_select(generate_putts_array(5), selected: 1), onclick: 'calculateGIR()' %>, put thecalculateGIR()in quotes.