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.
before it's submitted and save the value into a variable
This is the realm of jquery
/javascript
-- anything on the client side is JS, not Rails.
Rails/Ruby is basically like PHP -- it runs on the server.
Although @Sudipta Mondal
's answer will help, you have to remember to bind your code to an event (typically change):
#app/assets/javascripts/application.js
$(document).on("change", "select#form_item", function(e){
$(this).attr("data-option-value", $(this).val()); // this sets the "data-option-value" to the value
});
Perhaps this isn't the context in which you intended; it should give you the ability to dynamically assign the relative value to the field.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments