views:

123

answers:

2

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.

+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
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
@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
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
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