tags:

views:

70

answers:

4

Consider the following piece of code :

<select>
    <option value="0">foo</option>
    <option value="1" selected="selected">bar</option>
</select>

If I select the first option and reload the page, the first option stays selected and the select does not switch to the second option. How can I force firefox to give priority to the html?

+2  A: 

Select the second option using Javascript on page load.

luvieere
+1  A: 

When you load your page the selected="selected" will say which option to highlight in the select so that should work... for the first time :D.

To reload the page you must click in the browser address bar and then hit enter or type CTRL+SHIFT+R.

If I remember correctly it has something to do with caching. Firefox will remember the values of the form elements and use those instead of doing a complete refresh which is useful if you've filled out half the form and don't want to be forced to start over again on a refresh.

However it creates other problems as you have seen. The following page describes some ways of handling this issue.

dpb
-1 As I said, it has to do with Firefox's memory (concerning forms), not cache. And I'm not going to hack every visitor's machine to press Ctrl+Shift+R... but thanks for answering anyway
greg0ire
+3  A: 

Good question. I think this can't be done using pure HTML.

You could try using JavaScript to reset the form. I don't know what it will reset to, but I guess it will do what you want:

 document.forms["formname"].reset();

needs to be triggered on the load event of course (or in ready() in jQuery).

Pekka
Thank you, this works : a full jQuery solution : jQuery("#pwet").each(function(){this.reset();});
greg0ire
@greg0ire, your code implies that you have multiple `#pwet` defined.. IDs should be unique .. if you have only one, you could use the more efficient `jQuery("#pwet")[0].reset();`.. but i am just nit-picking :)
Gaby
I do not have multiple pwet defined, but this piece of code wouldn't work : jQuery("#pwet").reset(), I don't know why . I found the workaround on this page : http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ Your solution works great and is more efficient, so +1BTW, this topic is all about nit-picking so feel free to do so!
greg0ire
+1  A: 

I've had this problem and my solution was using PHP to give each name a random 5 digit key, it stops firefox remembering parts of forms. for example:

<input type="text" name="35472username"></input>
<input type="password" name="56784password"></input>

then when processing the form i would substr() the name.

It works well but there may be more efficient methods

Haroldo
Nice solution, but incompatible with caching, and it requires additional code to filter out the random digit key if you want to find your data...
greg0ire