I have the need to show the DropDown (HTML select element) expanded initially when it is loaded so that all the options in the DropDown are shown. I searched quite a bit, but suprisingly, I did not find an answer to such a simple problem. I am using ASP.NET MVC with jquery.
views:
123answers:
2
+1
Q:
How to show dropdown expanded automatically so that it displays all the options when page loaded
+1
A:
Here's a little mashup (that needs tweaking and optimising) but should get you started:
<div style="position:relative">
<select style="position: absolute">
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
</select>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("select").one("focus", function() {
this.size = this.options.length;
}).one("click", function() {
this.size = 1;
}).one("blur", function() {
this.size = this.options.length;
}).focus();
});
</script>
karim79
2010-02-18 10:16:25
Thanks for the solution. The solution is working but I have to set the size back on mouse click anywhere else. But I can easily add that.
mohang
2010-02-18 10:37:44
@mohang - Ah, I just added a `blur` event handler that's bound once using `one` to do that (which might be a little neater).
karim79
2010-02-18 10:52:18
A:
You could write in your HTML code:
<select size="3" name="test" id="test">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
and then via javascript set a timer that after while changes the select box size attribute to 1. For instance at the end of your page (before the clsed body tag) place this code:
<script type="text/javascript">
window.setTimeout(function() { document.getElementById('test').size = 1; }, 5000); //5000 = 5 seconds
</script>
Marco Demajo
2010-02-18 10:18:15
This is good enough for letting the user know that the list is populated. Does not work for me. But thanks for the effort.
mohang
2010-02-18 10:49:54