James Ding James Ding - 7 months ago 58
Javascript Question

Ruby on rails: select_tag how to display based on selected value

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 ?


select_tag("a", options_for_select(my_option_list))

will have the value just after user submits this. How can I get the value before they submit?


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>

    $('#my_select').on('change', function(){
        alert('do soemthing ' + $(this).val());