Topic: Selected item in a select box

I suspect this is a browser issue rather than a rails issue.  I'm creating a select tag in rails and it's correctly putting

selected="selected"

by the appropriate option.  However when I view the page (in Firefox at the moment - haven't got any other browsers installed on this machine at present) the item showing in the select box is always the top item and not the one with the selected code.  Any idea why?  This is really infuriating!

Re: Selected item in a select box

Does more than one menu item have the selected attribute?

Please paste the HTML output for the select menu here too.

Railscasts - Free Ruby on Rails Screencasts

Re: Selected item in a select box

I've actually changed the code to use a multi-select box now!  But no, only one menu item had the selected attribute.  I may have been using the select box elsewhere but it's hometime now so I'll check tomorrow.

Thanks.

Re: Selected item in a select box

I have a multi select where more than one items can be set to selected="selected".

Looks like the following:

<%= select_tag 'some_name[]', 
  options_for_select( [["foo", "1"], ["bar", "2"]], [1, 2] ),
  { :class => 'some-css', :multiple => true } %>

That sets the correct selected="selected", but Firefox does not show the two selected items. IE6/7, Opera9 and Netscape8 do it.

So you are right, it's more a browser issue :-) having browser compatibility issues can be a pain in the a...

EDIT: just adjusted the code. Made 2 little mistakes as I wrote it out of my head. First, to select multiple items you need an array ("some_name[]", not just "some_name"). Secondly, since this build upon a regular select box, you need to assign a class to it that defines the width and height - otherwise it'll just be displayed as regular select box with 1 single line.

Last edited by rudionrails (2007-03-13 07:13:08)

Re: Selected item in a select box

I hear you, I hate browser incompatibility issues.

Can you post the HTML that is generated? That way I can test it out.

Railscasts - Free Ruby on Rails Screencasts

Re: Selected item in a select box

The generated HTML is:

<select class="some-css" multiple="multiple" name="some_name[]">
<option value="1" selected="selected">foo</option>
<option value="2" selected="selected">bar</option>
</select>

I use that quite a lot - options_for_select is nicely explained in the api. One could probably use collection_select instead, but I have not tried that yet.

And like I said, the selected highlighting works in most browsers, but unfortunately not Firefox. From a professional perspective, however, it's fine as long as it works in IE6 and 7 as most of the users still use IE6. I can hear the voices now shouting about browser intercompatibility, but the majority of our clients just know enough to turn their PC on - people working in the field probably know what I am talking about smile

Re: Selected item in a select box

I tried pasting that HTML in a static HTML file and opening it in Firefox. It's working for me (selecting both options). I recommend you do the same to make sure your firefox is working properly.

Do you have anything else on that page which may be conflicting with this? Any javascript perhaps?

Railscasts - Free Ruby on Rails Screencasts

Re: Selected item in a select box

Yes, you are right. It does work when you copy and paste the HTML. I does not, however, when you use the Rails code, although the source HTML is identical.

Does Rails renders the page differently that may cause this error? Would be cool if there's a solution to this.

Re: Selected item in a select box

Weird. It may have something to do with the HTTP headers which are sent back. But I don't see how that would affect the select box.

Try validating the page to make sure there's no obvious problems (tags left open, etc.). But even then I imagine it would behave the same whether you paste the HTML directly or it is returned from a Rails site.

Railscasts - Free Ruby on Rails Screencasts

Re: Selected item in a select box

I've really exchanged the code to use a multi-select box now! But no, just one menu item had the picked out attribute.

Re: Selected item in a select box

I know this is an old post, but perhaps it will help someone to know:

Firefox likes to retain select box values even on page reloads. If you +shift reload (a non cached refresh) the page will load up as new and

Re: Selected item in a select box

As per my opinion, you have to set property like selected index for application.