2

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.

CottonEyeJoe
  • 680
  • 1
  • 7
  • 24
  • if you are submitting using a form, what is your purpose of saving it into a variable. You can save the value simply using jQuery or Javascript if you want to save it your DOM. If you want to save it in your database, you can access the values which are posted to your rails application from the form – Sudipta Mondal Dec 10 '15 at 12:31
  • @SudiptaMondal I want to catch & save the value into a variable, before the form gets submitted. This allows my form fields to respond dynamically (e.g. fade in new form fields, if the one before has value X). – CottonEyeJoe Dec 10 '15 at 12:45

2 Answers2

2

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 :)

Sudipta Mondal
  • 2,470
  • 1
  • 17
  • 20
  • Thank you. How would the code look like, if I want to save the output into a variable? – CottonEyeJoe Dec 10 '15 at 13:12
  • the variable in code above *form_field_value* is only going to store the data of your field. you can use it later in an if condition to display another form `if (form_field_value == "X") {//display new form}` – Sudipta Mondal Dec 10 '15 at 13:30
  • @Harakiri could you please mark my answer as accepted, if you are satisfied with the solution. – Sudipta Mondal Dec 10 '15 at 16:51
1

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.

Richard Peck
  • 73,250
  • 8
  • 84
  • 139
  • I've saved your JavaScript into the application.js file. Now I want to fill in the data-option-value with the respective value. But I get a `undefined local variable or method field` error, when I try to do this `data-option-value=""` Any Ideas? Thank you – CottonEyeJoe Dec 14 '15 at 09:40
  • Where are you getting `` from? – Richard Peck Dec 14 '15 at 09:41
  • I thought that `var field = $(this).val();` can be read by this? – CottonEyeJoe Dec 14 '15 at 09:48
  • `$(this)` is javascript, whereas `` is Rails - they won't work together as you need. If you explain a little more, I can give you more context – Richard Peck Dec 14 '15 at 09:57
  • I just want to use the value of a form as it gets selected in the dropdown inside my Rails views before the form gets submitted. – CottonEyeJoe Dec 14 '15 at 10:03
  • Well my above code provides it. You get a *javascript* variable called `field`, with which you're able to do whatever you want. Which element are you using `data-option-value` for? - I'll give you some code to get it working if you want – Richard Peck Dec 14 '15 at 10:05
  • @Rick Peck it's more or less connected to [this](http://stackoverflow.com/questions/34262356/rails-form-fields-loops) question. I am trying to hand over a select-form field value, which allows my rails app to hide or show specific form fields. – CottonEyeJoe Dec 14 '15 at 10:22
  • Hmmm okay - do you have more context? Such as how do you want Rails to determine which forms to show -- through ajax or on the same form? – Richard Peck Dec 14 '15 at 10:34
  • I use [this](https://github.com/vollnhals/dependent-fields-rails) gem. It's more or less JavaScript. I want to show select-form-fields only when a specific value gets selected from the select-form-field above. E.g. If i select Adidas (Model: Brand) then a second Select-form appears (Model: Groups) where I can choose between Shoes, Shirts, .. The Brand and the Model group have the same column (Brand), so I can connect them through the models. – CottonEyeJoe Dec 14 '15 at 10:48
  • Nice okay, this is making progress. So you just need to update the element to have `data-option-value` as the field value? – Richard Peck Dec 14 '15 at 10:50
  • Yes, exactly. I want to manipulate the data-option-value to the value that was selected before and store this value in a global variable, so I can use it in another form field. – CottonEyeJoe Dec 14 '15 at 10:55
  • Global var? Why not just update the other field from the `.change` function? – Richard Peck Dec 14 '15 at 10:55
  • Sorry. Hard to describe :). The value from the first select form should be saved in a global variable before the form gets submitted so I can access the value in my rails app. E.g. to use it in other select form attributes. It's a bit complicated and special. But it allows my app to show dependent form fields that are depending on the form field values from other form fields. – CottonEyeJoe Dec 14 '15 at 11:02
  • Exactly! That's what I need. – CottonEyeJoe Dec 14 '15 at 12:18
  • Okay, have you got the other actions set up or do you need me to suggest how to do those... otherwise I'll just give you some info on this functionality? – Richard Peck Dec 14 '15 at 12:19
  • I haven't changed anything yet. Especially the suggestes JavaScript part seems very tricky if one want to use it with rails. Feel free to go ahead. I'm so glad that you're helping me with that! – CottonEyeJoe Dec 14 '15 at 12:27