I have question that seems very simple, but I just can\'t get it right. I have a with a list of options and a default value. After the user selec
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>
$('#select').val('defaultValue');
$('#select').change();
This will also trigger events hooked to this select
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.
Once you have done your processing in the selectFunction() you could do the following
document.getElementById('select').selectedIndex = 0;
document.getElementById('select').value = 'Default';
document.getElementById("select").selectedIndex = 0
will work
If you would like it to go back to first option try this:
document.getElementById("select").selectedIndex = 0;