82

I have question that seems very simple, but I just can't get it right. I have a <select> with a list of options and a default value. After the user selects an option and clicks on a button, I want the select to go back to its default value.

<select id="select">
 <option value="defaultValue">Default</option>
 <option value="Option1">Option1</option>
 <option value="Option2">Option2</option>    
</select>
<input type="button" value="CHANGE" onclick="selectFunction()" />

So lets suppose the user selects Option 1. I get that value with javascript but after that, I want the select to show again "Default".

I found that doing document.getElementById("select").value = "defaultValue" won't work.

Does anyone know how to change that value back to the default?

2

13 Answers 13

100

Setting .value to the value of one of the options works on all vaguely-current browsers. On very old browsers, you used to have to set the selectedIndex:

document.getElementById("select").selectedIndex = 0;

If neither that nor your original code is working, I wonder if you might be using IE and have something else on the page creating something called "select"? (Either as a name or as a global variable?) Because some versions of IE have a problem where they conflate namespaces. Try changing the select's id to "fluglehorn" and if that works, you know that's the problem.

Sign up to request clarification or add additional context in comments.

3 Comments

What if the option of index 0 is disabled?
@MemphisMeng - Then adjust accordingly.
@MemphisMeng @T.J. Crowder i am using this solution (thanks btw) and resetting selectedIndex to 0 with a disabled option at 0 works fine
14

I found that doing document.getElementById("select").value = "defaultValue" wont work.

You must be experiencing a separate bug, as this works fine in this live demo.

And here's the full working code in case you are interested:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        var selectFunction = function() {
            document.getElementById("select").value = "defaultValue";
        };
    </script>
</head>
<body>
    <select id="select">
        <option value="defaultValue">Default</option>
        <option value="Option1">Option1</option>
        <option value="Option2">Option2</option>    
    </select>
    <input type="button" value="CHANGE" onclick="selectFunction()" />
</body>
</html>

2 Comments

Is it possible to select a value which is not into the options?
It might work in chrome, firefox but i am not able to get the selected value in the drop down in some versions of IE. tried to test in IE9 and IE 11 and ended up with the same problem.
13
$('#select').val('defaultValue'); 
$('#select').change();

This will also trigger events hooked to this select

1 Comment

Or $('#select').trigger('change');
7

document.getElementById("select").selectedIndex = 0 will work

3 Comments

I want the SELECT to display again the default value. I did that, and the display does not show the default value again
It'will if the default option is at index 0. It its in other index use that index. See jsfiddle.net/gN3ke/2 here I have used Option2 as default. And set selectedIndex to 2
sorry it's not working in 2019 || can you provide any working example?
6

Once you have done your processing in the selectFunction() you could do the following

document.getElementById('select').selectedIndex = 0;
document.getElementById('select').value = 'Default';

Comments

3

If you would like it to go back to first option try this:

   document.getElementById("select").selectedIndex = 0;

Comments

3

I think I know what the problem is here - if you are trying to have it select an option that doesn't exist, it won't work. You need to ADD the option first. Like so:

var option=document.createElement("option");
option.text=document.getElementById('other_referee').value
document.getElementById('referee_new').add(option,null);
document.getElementById('referee_new').value = document.getElementById('other_referee').value;

Not elegant, but you get the idea.

Comments

2
 <script language="javascript" type="text/javascript">
        function selectFunction() {
             var printStr = document.getElementById("select").options[0].value
            alert(printStr);
            document.getElementById("select").selectedIndex = 0;
        }
    </script>

Comments

2

This solved my issue, it was working fine with

var value = item[index].id;
$("#select2").val(value);

but not showing selected value, so

$("#select2").val(value).change();

OR

$("#select2").val(value).trigger('change');

works fine for me.

Comments

1

You can use the selectedIndex property to set it to the first option:

document.getElementById("select").selectedIndex = 0;

Comments

1

HTML

<div class="form-group" id="divnacionalidad">                                                               
    <label>Nacionalidad:</label>                                                            
    <select id="nacionalidad" class="form-control">                                                               
        <option value="0">Seleccione una opcion</option>
        <option value="2">Costa Rica</option>
        <option value="1">Nicaragua</option>
    </select>
</div>

Javascript:

function buscarIndice(caden, valor) {
    let cadena = document.getElementById(caden);
    for (let i = 0; i < cadena.children.length; i++) {
        if (cadena.children.item(i).textContent.trim() === valor.trim()) {
            cadena.options.item(i).selected = 'selected';
        }
    }
}

3 Comments

El presente codigo, utilizara un select que radica en un html y mediante esa funcion se permite un intercambio de selección en dependencia de un texto ya establecido en los option.
Please use edit link instead of adding information in the comments. Also, please write your answer in English, as Stack Overflow is an English site.
Algo importante los value="Numero" de los option, tiene que estar ordenado de forma ascendente ( 0,1,2,3...) Para que funcione.
1

This question is old but I was having the same issue and my answer may help a beginner like me. In this example, Option1 is the default value shown in the select.

<select id="myselect">
    <option value = "opt1">Option1</option>
    <option value = "opt2">Option2</option>
</select>

In my case, I made a mistake trying to change the select value with the text shown in the select "Option1" and not with the actual value "opt1" So when trying to change the value with javascript this way

document.getElementById('myselect').value = 'Option1';

I got an empty select.

Instead, I used :

document.getElementById('myselect').value = 'opt1';

I hope this helps.

Comments

0

We can iterate the option to find and set the selected attribute on the respective option.

document.getElementsById('select').childNodes.forEach((e, i, a) => {
    if (e.value == "<the value you want to be selected>") {
        e.selected = "selected";
    }
});

For the original option just use its value content.

3 Comments

you should check for comparison of the e variable not a.
@LewisMorris thanks, i missed the typo. fyi you can always use the edit function to propose edits directly
fair play, will do in future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.