code master
code master

Reputation: 2026

How to reset dropdown list <select> on 'back' button of browser using Javascript?

How to reset (back to 0 index) dropdown list on 'back' button of browser using Javascript?

Upvotes: 14

Views: 44564

Answers (3)

pearl's
pearl's

Reputation: 61

yes its easy.

i have a dropbox and its id was "stroke_style" the reset is just to set it at -1 index (before 0)

document.getElementById("stroke_style").selectedIndex = -1;

example: http://languagelassi.blogspot.in/search/label/Dropdown%20Reset%20Javascript

and you can put it in a method or directly <body onload=""> of that page

Upvotes: 6

code master
code master

Reputation: 2026

<script type="text/javascript" language="javascript" >

var orig_default = -1;

function setDefault() {
if (orig_default < 0) {orig_default = document.getElementById("MyList").selectedIndex;}
}

function testReset() {
if (orig_default >= 0) {
    document.getElementById("MyList").selectedIndex = orig_default;
 }
}
</script>
<body onbeforeunload='setDefault();'>

Here are couple of scripts which I have written myself to solve the issue of resetting the value of html upon browser back button click.

Upvotes: 0

Don
Don

Reputation: 17636

You can use the 'onbeforeunload' event:

<script>
function reset_options() {
    document.getElementById('MySelect').options.length = 0;
    return true;
}

</script>
<body onbeforeunload='reset_options()'>
...

Upvotes: 8

Related Questions