Ruby on Rails: Get the value of a selected form field before it is submitted

advertisements

I'm trying to get the value of a select form field, before it's submitted and save the value into a variable. I know that you can't do it with Rails only. So JavaScript/Ajax will do the trick. I'm pretty new to Rails, so I hope you can help.


Using Javascript

var form_field_value = document.getElementById("pass-id").value

Using jQuery

var form_field_value = $("#pass-id").val()

Each of the above ways, will save the value in your DOM, so you can access it later. Do make sure, of the scope of the variables, when you are declaring them :)