0

I have a drop down menu like below:

<select onChange="window.open(this.value)">
<option selected="selected" value="">--select an url--</option>
<option value="URL1">URL1</option>
<option value="URL2">URL2</option>
<option value="URL3">URL3</option>
</select>

I would like to have the dropdown menu return to "--select an url--" option, instead of URL option, everytime after a selection was made.

Can someone show me how to implement the codes? Thanks.

aynber
  • 22,380
  • 8
  • 50
  • 63
Jkil
  • 19
  • 7
  • 1
    Possible duplicate of [Set select option 'selected', by value](https://stackoverflow.com/questions/13343566/set-select-option-selected-by-value) – Alon Eitan Sep 11 '18 at 07:29
  • I am assuming you are using jquery - Please use the below code to reset it back to the initial selection $('select').val('initialvalue') – IamChandu Sep 11 '18 at 07:32

2 Answers2

2

Just add this to the onChange event:

this.value = '';

So the code would look like:

<select onChange="window.open(this.value);this.value = '';">
  <option selected="selected" value="">--select an url--</option>
  <option value="URL1">URL1</option>
  <option value="URL2">URL2</option>
  <option value="URL3">URL3</option>
</select>

This is assuming the '--select an url--' has an empty option value i.e. value=""

Matthew P
  • 717
  • 1
  • 7
  • 22
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="dropDownBox">
<option selected="selected" value="">--select an url--</option>
<option value="URL1">URL1</option>
<option value="URL2">URL2</option>
<option value="URL3">URL3</option>
</select>

<script>
$("#dropDownBox").change(function() {
   var test = $(this).val();
   $(this).val("");
   window.open(test);
});
</script>
uday
  • 13
  • 5