views:

72

answers:

2

Hi

I'am using select_tag and I want to pass selected value in href :onchange

I couldnt fetch the selected value

Please guide me how to fetch the selected value and pass in href

I'am using following code

<%= select_tag "name", 
   options_for_select(@user.names.map {|c| [c.firstname,c.id]}),
   :onchange =>"document.location.href='/names/value'" %>

In the code in href='/names/**value** at the place of value I have to pass selected value

Thanks

A: 

I'm not sure to understand your question but you can try this:

<%= select_tag "name", options_for_select(@user.names.map {|c| [c.firstname,c.id]}),:onchange =>"document.location.href='/names/'+this.value" %>

Thereby, the selected value is used during the onchange event.

Kiva
+this.value is printing as it is in the path, getting no value
nirmal
Can you give us the html printed ?
Kiva
html page is too big...iam using this select_tag inside form_tag,and :onchange inside select_tag, it should take me to form with different valuefor example if page path is http://localhost:3000/names/1onclick of selected item with value 2, it should take me to pagehttp://localhost:3000/names/2
nirmal
I just want to see the html for the select_tag ;) with options
Kiva
<select id="name" name="name" onchange="document.location.href='/names/+this.value/males/edit"><option value="1">Ashok</option><option value="2">bala</option></select>
nirmal
i even tried putting value inside {} like #{+this.value}, and no answer
nirmal
Hmmm, ok, do this in your :onchange => "document.location.href='/names/'+this.value+'/males/edit'"And try to change value in your page.Onchange is javascript option, so it's normal 'this.value' is printed.
Kiva
it is still printing '+this.value+' as it is
nirmal
In your html, it's normal because is a javascript option.When you tried to change your value in your select, are you redirect to another page ?
Kiva
yes, just the value will be changing, any way its different page
nirmal
thanks for responding, please if u find the answer post it
nirmal
A: 

Try this

<%= select_tag "name", 
   options_for_select(@user.names.map {|c| [c.firstname,c.id]}),
   :onchange => "document.location.href=/names/ + $(this).value" %>
j.
no + $(this).value is printing as it is, please tell me if any other way to do this
nirmal
But did you try using the select to see what happens? I've tried here and it worked, and it's also printing `+ $(this).value`. Try using the select...
j.
i used like this<%= select :name, @user.names.map {|c| [c.firstname,c.id]}, {:onchange => "document.location.href=/names/ + $(this).value"} %>but i dont get onchange event
nirmal
It worked for me :/
j.
Are you using firebug? Does it show any errors?
j.
it dosent show any error, but onchange is not enabled
nirmal