Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't keep previously selected value in select box when page is reloaded

Tags:

html

select

Deos anybody know how to prevent the browser from keeping the last selected option when the client reloads the page?

The problem is that when I reload the page, it keeps the last selected option, and when I select the url in the address bar and hit Enter it will be reset.

I want the second result whicth means that I want to get the browser always reset the select box.

Please the simplest and the safest way :)

Thanks.

like image 889
medk Avatar asked Jan 20 '23 23:01

medk


2 Answers

The autocomplete="off" attribute works on select elements as well as on input elements:

<select autocomplete="off" >
     <option>1</option>
     <option selected>2</option>
</select>
like image 183
manu Avatar answered Jan 23 '23 14:01

manu


Just set the value in javascript. That way when the page is loaded its always initialized to the same value. Just using <option selected="selected"> won't work.

in javascript it would be something like this:

document.getElementById("selectBoxToBeReset").options[indexToBeSelected].selected = true;

and jquery helps even more:

$("#selectBoxToBeReset").selectOptions("value to be selected", true);

passing true as the second argument clears any previously selected value. Jquery select box tips.

like image 32
Gordon Gustafson Avatar answered Jan 23 '23 13:01

Gordon Gustafson