136

If you change a dropdown and refresh the page, Firefox seems to ignore the selected attribute.

<option selected="selected" value="Test">Test</option>

It will in fact select the option you had previously selected (before the refresh). This ends up being a problem for me since there is an event triggered on the dropdown which changes other things.

Is there a way to make firefox stop this behavior (other than firing another event when the page loads)?

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
monkey-wrench
  • 1,556
  • 2
  • 14
  • 16

20 Answers20

348

Add autocomplete="off" HTML attribute to every select tag.

(source: https://stackoverflow.com/a/8258154/260080)

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
91

In firefox, I've notice that the "selected" attribute will not work unless you place the select inside a form, where the form has a name attribute.

user1707970
  • 911
  • 6
  • 3
12

Just had the same issue, believe me it's been more than 10 hours struggling with this stupid firefox behavior, i have 7 dropdowns, each of them will trigger an event and fill in 24 hidden inputs, so you can imagine having the right option selected with 24 wrong input values!!! the solution i finally found is to reset the form with Javascript adding this line of code:

window.onload = function() { document.forms['MarkerForm'].reset(); };

PS: the inputs have the values pulled from a database, so resetting the form does not empty any value but in a way tells firefox to go back the hell to selected=selected option!

  • This is the correct answer. The answers saying to use "autocomplete" on a select element are wrong because "autocomplete" is NOT a valid attribute for a select field according to W3 and will cause "Attribute autocomplete not allowed on element select at this point." errors on validation. – Scott Oct 22 '15 at 18:41
11

It's just Firefox remembering your previous selection when refreshing. Try a hard refresh instead.

Also, same issue over here: https://stackoverflow.com/a/1505693/1069232

Also see here: https://bugzilla.mozilla.org/show_bug.cgi?id=274795

Community
  • 1
  • 1
hammygoonan
  • 2,125
  • 2
  • 21
  • 36
7

AFAIK, this behaviour is hard-coded into Firefox.

You could try setting each form element to its defaultValue on page load.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
5

Try to disable autocomplete attribute of select input ... sometimes browser ignore select because of that

Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
3

I'm using FF 25.0.1

It ignore selected="" and selected="selected".

But if I simply try selected the option is selected.

Strange (non conformant) behaviour. I know selected is valid HTML5 and it's the shortest form, but I usually write code that also validates as wellformed XML, so that I can use any XML validation tool to check my results in a very strict way (and data exchange is very easy...)

According to W3C, these variants should be valid on boolean attributes:

HTML5:  boolAttr="" | boolAttr="boolAttr" | boolAttr
XHTML5: boolAttr="" | boolAttr="boolAttr"

I prefer the first one, as is it nearly as short as the last (not xml conformant) variant but should validate as both XHTML5 AND HTML5. So I hope, Mozilla will fix it!

AlexB
  • 7,302
  • 12
  • 56
  • 74
Michael
  • 181
  • 2
  • 3
3

Try giving the dropdown a name.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
FourtyTwo
  • 734
  • 8
  • 19
3

enclose select in form attribute and it will work.

<!-- will not work in firefox -->
<option selected="selected" value="Test">Test</option>

and

<!-- this will work in firefox -->
<form>
 <option selected="selected" value="Test">Test</option>
</form>
Muhammad Tahir
  • 2,351
  • 29
  • 25
3

use .prop() instead of .attr()

This does not work in firefox.
  $( 'option[value="myVal"]' ).attr( 'selected', 'selected' );
use this one
  $( 'option[value="myVal"]' ).prop( 'selected', 'selected' );

In other way
  $( this ).prop( 'selected', 'selected' );
Zaheer Babar
  • 1,636
  • 1
  • 15
  • 17
  • Using `.prop('selected', true);` or `.prop('selected', false);` (jQuery) to enable/disable works for both Firefox and Chrome. – JimB Jan 22 '16 at 16:19
2

With the name is better

form id="UMForm" name="UMForm" class="form"

The select will take the selected attribute

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
snk
  • 21
  • 1
2

You could call .reset() on the form before refreshing the page.

Neil
  • 54,642
  • 8
  • 60
  • 72
1

For me none of the solutions above worked. I had to explicitly set the selection if none was set:

if (foo.find(':selected').length === 0) {
    $(foo.find('option')[0]).attr('selected', 'selected');
}
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
boxed
  • 3,895
  • 2
  • 24
  • 26
1

autocomplete wasn't working for me either.

This is the javscript fix written in jquery that i use:

$('input[type="radio"][selected]').click();
1

To show the first item of the dropdown, use ProjectName.ClearSelection();

Put lines in your design page to work on all browser And also put this on code behind on page load.

$(document).ready(function () {
    $("#content_ProjectName option[value='1']").prop("selected", true);
});
TylerH
  • 20,799
  • 66
  • 75
  • 101
Chinmaya
  • 11
  • 1
1
<option selected="selected" value="Test">Test</option>

In this case this worked both for Chrome and Firefox.

$('option[value="Test"]').prop('selected', true);

I was using .attr() instead of .prop()

Shapi
  • 5,493
  • 4
  • 28
  • 39
1

If you change the select and refresh the page firefox will restore your changes on the form, that's why you feel like the select isn't working. Instead of refreshing, try opening the link on a new tab.

Rod
  • 2,046
  • 2
  • 20
  • 24
  • This is true, however doesn't seem to solve my problem. I still need a work around since my onchange event doesn't fire when firefox restores any changes to the form when I do a refresh. – monkey-wrench Jan 28 '11 at 18:57
0

This is my solution:

var select = document.getElementById('my_select');
for(var i=0; i < select.options.length; i++){
    select.options[i].selected = select.options[i].attributes.selected != undefined;
}

I just put that at the top of the page (with appropriate id set), and it works for me. Replacing the getElementById with a loop over all selects on the page, I leave as an exercise for the reader ;).

Benubird
  • 18,551
  • 27
  • 90
  • 141
0

At work, I just fixed a bug where the select box option displayed properly in Chrome but not in Firefox, on the same web page. It turned out to be something completely different than the problems above, but could possibly be a problem you are experiencing.

In Chrome, the select box font-color was black. For some reason in Firefox, the select box inherited the font-color from the container, which was white. Once I added a CSS rule to force that select box font-color to be black, the value set was properly displayed.

Stefan Musarra
  • 1,429
  • 14
  • 16
0

Neither autocomplete="off" or placing it inside a form works for me.

What worked was to only use the attribute selected with no "value" like this:

<option @(Model.Source == TermSource.Instagram ? "selected" : "")>
    Instagram
</option>
<option @(Model.Source == TermSource.Facebook ? "selected" : "")>
    Facebook
</option>

so either it renders <option selected>...</option>, or just <option>...</option>

uggeh
  • 713
  • 4
  • 6