0

So I have this javascript variable I'm generating on my html.erb views that I want to send to a rails variable the form submit. I understand that I can do this using jquery or ajax but my attempts have failed. Here is what I am trying:

Rails variable:

<%= f.text_field :antennatime, :id => "antenna_time" %> 

Some HTML:

<input type="checkbox" id="Antenna" onclick="fun();" value="1"> 

My javascript:

<script> var str = "This is Some Sample Text"; var ant = document.getElementById("Antenna").checked; if(antenna == true){ $(document).ready(function(){ $("antenna_time").val(str); }); } </script> 

To my expectations this is not working. Could someone show the steps I should use to use jquery or ajax to send my javascript var str to my rails variable :antennatime?

    3 Answers 3

    1

    It looks like you've misspelled a variable and missed a # from your jQuery selector:

    var str = "This is Some Sample Text"; var ant = document.getElementById("Antenna"); if (ant.checked){ $(function(){ // same as $(document).ready() $('#antenna_time').val(str); }); } 

    For clarity, using an if statement is for checking expressions/evaluating - I would add your .checked logic there as it's more clear what's happening.

      1

      Your question is a bit confusing. Let me paraphrase to make sure I understand: you have a form, of which one field is filled with a attribute of a model, and when a checkbox is set, you want to set the value of that field, using javascript.

      So in reality, you are not setting some "rails variable" but you are changing one field in a form, before submitting it to the server.

      So, in that case it is simple, you just omitted the # in your jquery. So you will have to write

      $('#antenna_time').val(str) 

      This will set the field to the requested value. But as per your code, it will do this, only when the page is loaded and when the checkbox is clicked.

      This might be what you want, but I would suspect you

      • want it to be set/unset when the checkbox is clicked? (the fun() function is not shown --speaking of cryptic names)
      • to be checked before submit only (possibly confusing for the user, and why not just check the value of the checkbox server-side instead then)

      Please note, to set a rails variable you will have to post something to the server.

      It is not entirely clear why you overwrite the id of the field, which imho is potentially dangerous, since rails uses the id/names to build the params hash to be posted to the rails server. The id of an input-field of an attribute in a model is always built as follows: <model-name>_<attribute-name> which is unique (and clear) enough imho to just use that.

      1
      • Apologizes for the confusion. I will try to be more clear in future questions. Thank you for your input about the id. I will use your advice.CommentedJan 15, 2014 at 19:45
      0

      You need to compare the ant variable not antenna since you've defined it above if condition as well as using # to target element with id named antenna_time

      Also wrap all your code inside DOM ready, final code look like this:

      $(document).ready(function(){ var str = "This is Some Sample Text"; var ant = document.getElementById("Antenna").checked; if(ant == true){ $("#antenna_time").val(str); } }); 

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.