1

Now my case might be a bit weird. I have a select_tag, user has to select one value from the list I gave. I know how to retrieve the value they selected once they click on the submit. But now I would also like to display some info for the selected value BEFORE they click on submit, just when they select any value in the drop down list.

So is there any way to get the value after user selects but before user submits ?

E.g:

select_tag("a", options_for_select(my_option_list))

the param[:a] will have the value just after user submits this. How can I get the value before they submit?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
James Ding
  • 313
  • 5
  • 7
  • You could refer to the link below to display the selected value next to the dropdown. http://stackoverflow.com/a/10520467/5625947 – Yewness Sep 12 '16 at 12:30

2 Answers2

1

It depends what you want to do but you could use javascript.

Check out the fiddle

<select id="my_select">
    <option value="value 1">Option 1</option>
    <option value="value 2">Option 2</option>
    <option value="value 3">Option 3</option>
    <option value="value 4">Option 4</option>
</select>


$(function(){
    $('#my_select').on('change', function(){
        alert('do soemthing ' + $(this).val());
    });
});
Ryan-Neal Mes
  • 6,003
  • 7
  • 52
  • 77
  • Also checkout http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false for preventing submit so you can do stuff first. – Ryan-Neal Mes Apr 22 '15 at 22:59
  • Thanks! What if I would like to show info on the page which is a column in a table ? – James Ding Apr 22 '15 at 23:06
  • That do you mean show info? If you wanted to updated something on the page with the text selected in the text box you would need to use javascript/jquery to find the element and change the text. Something like `$("p#my-paragraph").text($(this).val())` – Ryan-Neal Mes Apr 22 '15 at 23:10
  • I have a hash table, the key is in the dropdown list, once user makes some choice, I would like to display the value of that key in the hash table. can u provide some details? – James Ding Apr 22 '15 at 23:32
0

You will need to use javascript/coffeescript for this. A sample of this might be something like this:

$(document).ready ->
  $("#id_of_select_that_the_select_tag_generates").change ->
    # do something

Put the above in app/assets/javascripts/some_file_name.js.coffee

Eugene
  • 4,829
  • 1
  • 24
  • 49