2

Can you help me? I want to open a selectbox with JS. But it does not work in Firefox. Do not know anyone help me how to solve?

<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>
<span class ="xxx">open</span>

jQuery(document).ready(function($) {

    $(".xxx").on('click', function () {
        openSelect($('#selId'));
    });


    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.");
        }
    }

});

DEMO - http://jsfiddle.net/uhYxf/

user2219071
  • 113
  • 10
  • possible duplicate of [How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?](http://stackoverflow.com/questions/249192/how-can-you-programmatically-tell-an-html-select-to-drop-down-for-example-due) – Alex W May 16 '13 at 12:38

1 Answers1

0

You will have a hard time to perform this cross-browser. I would opt for using a select box plugin like select2 or Chosen instead of using an html <select> element.

However, another option which would be easier is to change the size property of the select for someting greater than 1 when clicked. That doesn't give the same feel, but could probably fit your needs.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • Thank you, but Select2 or Chosen generates div element, but I need SelectBox element. There is no other way? – user2219071 May 16 '13 at 12:53
  • @user2219071, Could you explain why you specificly need a ` – plalx May 16 '13 at 16:25