9

I have a select element as follows. i want to open it without user having to click on it.

    <select id="selId" class="pos_fixed">
       <option value="volvo">Option1</option>
       <option value="saab">Option2</option>
       <option value="mercedes">Option3</option>
       <option value="audi">Option4</option>
    </select>

Please let me know if its possible using jquery or javascript

Parag A
  • 443
  • 2
  • 8
  • 20

5 Answers5

19

You will pass a CSS selector to openSelect() and it will open the select element for you.

var openSelect = function(selector){
     var element = $(selector)[0], worked = false;
    if (document.createEvent) { // all browsers
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        worked = element.dispatchEvent(e);
    } else if (element.fireEvent) { // ie
        worked = element.fireEvent("onmousedown");
    }
    if (!worked) { // unknown browser / error
        alert("It didn't worked in your browser.");
    }   
}

$(function(){ // when DOM is ready
   // open .select element
   openSelect('.select'); 
});

Here's a fiddle: http://jsfiddle.net/Z48wF/1/

Source: How to open the select input using jquery @stackoverflow.com

Community
  • 1
  • 1
kyle.stearns
  • 2,326
  • 21
  • 30
  • 14
    Only works in WebKit browsers (Safari, Chrome). Doesn't work in Firefox, Opera and IE. I just tested it. – zengabor Apr 22 '13 at 22:21
  • 1
    And it won't work anymore in Chrome either in a month: https://www.chromestatus.com/features/5718803933560832 – Capsule Aug 03 '16 at 05:14
1

You can find a similar question here: How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?

I don't think there is a single solution that would work in every browser.

I can confirm that using document.createEvent() and .dispatchEvent() (as explained at the above link) works great in WebKit browsers (Safari, Chrome) but not in Firefox, Opera and IE.

However, you can try combining the different solutions listed there.

Community
  • 1
  • 1
zengabor
  • 2,013
  • 3
  • 23
  • 28
  • 1
    You should have stopped at the second paragraph. :-) There is no reliable, cross–browser way to "open" a select element. – RobG Apr 22 '13 at 22:29
0

I have a complete solution for this problem here Is it possible to use JS to open an HTML select to show its option list?. In this scheme the select can be opened correctly, easy, with all its functionality and preserving the page layout. That solution is safe, simple and compatible with Internet Explorer, FireFox and Chrome.

Thank's!

Community
  • 1
  • 1
Eduardo Lucio
  • 1,771
  • 2
  • 25
  • 43
-3
$(document).ready(function(){  
  $('#selId').on('click',function(){
     //do stuff here
  });
  $('#selId').trigger('click');
});

This should work.

Update:

$(document).ready(function(){  
  $('#selId').click(function(){
     //do stuff here
  });
  $('#selId').click();
});

Very similar to the other answer, but that should do it also rather then "click" you can try "focus"

Jordan Ramstad
  • 169
  • 3
  • 8
  • 37
-3

You can use the following script

<script>
    function(){
        selectbox = $(select-selector)[0]
        selectbox.size = selectbox.options.length;
    }
</script>
Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57