Santosh Aryal
Santosh Aryal

Reputation: 1376

Pass value of JavaScript variable to Rails view with hidden_field_tag helper

I set up a JavaScript program using jQuery to change the user input values at various form field.

$(document).ready(function(){
    $("#billAmt").keyup(function(){
        var a = $("#billAmt").val();
        var b = a*3/100;
        var d = "<%= current_user.balance %>";
        var c = d - b;
        $("#cashBack").val(b);
        $("#total").val(c);
    });
});

This JavaScript code is inside the form_tag and I would like to pass #cashBack to hidden field.

I did like:

<%= hidden_field_tag :cashBack, nil, :id => "cashBack", :value => ''%>

When I submit the form, params[:cashBack] is empty. So, how do I pass the value and change every time I change the value.

Upvotes: 2

Views: 1503

Answers (1)

GilmoreGarland
GilmoreGarland

Reputation: 76

In order to set hidden textfield, You must use pure javascript instead of JQuery.

$("#cashBack").get(0).value = b;                  

Upvotes: 5

Related Questions