0

I have a form like this,

<html>
<body>
<form>
<select name="alphabets" id="alphabets">
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="D" selected="selected">D</option>
</select>
</form>
</body>
</html>

When I pull the page the first time, option D is selected. If I select option A from the dropdown, and then do a page refresh, I want the dropdown option to go back to D. On page refresh, I want everything to go back to it's initial state. But the dropdown displays option A, or whatever was previously selected.

Any idea how can I make the page display option D on page refresh?

Thanks.

user471317
  • 1,231
  • 2
  • 22
  • 35
  • 1
    Are you refreshing in Firefox? It is the only browser I've ever worked in that kept form data on page refresh. http://stackoverflow.com/questions/1479233/why-doesnt-firefox-show-the-correct-default-select-option – TheZ Jul 10 '12 at 22:55
  • Have you tried the following in a document ready handler (or in a script block that appears after your select): `$("#alphabets").val("D")` – nnnnnn Jul 10 '12 at 22:56

3 Answers3

5

Place the following in a script tag on your page.

 $(function(){
      $('#alphabets option[value="D"]').attr('selected', true);
 });
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
0
$(document).ready(function() {
    $("#alphabets").val(localStorage.alphabets);
});

$("#alphabets").change(function() {
    localStorage.alphabets = $(this).val();
});

I think this will work (only in browsers that support localStorage) but I haven't tested it.

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
0

This is a 'feature' with Firefox remembering selection on select lists.

Add autocomplete="off" to the <select> tag.

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100