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
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();
works fine for me.
You can use the selectedIndex property to set it to the first option:
document.getElementById("select").selectedIndex = 0;
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.
<script language="javascript" type="text/javascript">
function selectFunction() {
var printStr = document.getElementById("select").options[0].value
alert(printStr);
document.getElementById("select").selectedIndex = 0;
}
</script>