11

So I can see the options of the select element but I need to click on it. What if I want to use a function? When something happens this select element should be selected, means the list is open and I can see the options. I don't want the user to click on the select element, I want something else to open it.

What I've tried

$("select").select();
$("select").click();
$("select").focus();

There is a select element, usually if you want it to open (the drop down list), you click on it. What I want is to open it if I click on a DIV or anything else. I want this drop down list to to open, without having the user clicking on the select element.

3
  • are you trying to open drop-down when you click on input Commented May 4, 2012 at 17:27
  • 1
    as far as I know you can't force a (native) <select> element to just "drop down". Commented May 4, 2012 at 17:30
  • checkout api.jqueryui.com/selectmenu/#method-open Commented Aug 14, 2017 at 11:31

3 Answers 3

23

This should work:

var element = $("select")[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.");
}

Example

Sign up to request clarification or add additional context in comments.

10 Comments

I tried the fiddle but how am I supposed to make it open by itself? Should I click somewhere?
It worked on Chrome, don't know why not on Firefox. (It didn't give me the alert)
This worked in Chrome, Safari. Did not work in IE9, IE10, Firefox, Mobile Safari (iPad).
Can u give reference link
|
5

Just an update here as the accepted answer is correct but outdated and initMouseEvent is soon to be deprecated. [See: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent]

Use the following instead: new MouseEvent [See: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent]

Example code:

var el = $('select'); // grab the input (jQuery)
var event = new MouseEvent('mousedown'); // create the event listener
el.dispatchEvent(event); // attach the event listener to the element

2 Comments

That developer mozilla links are not working anymore.
you have to select the DOM element var el = $('select')[0], because dispatchEvent() is not a jQuery method. For me this still doesn't work anyways..
1

I dont think you can force the select box to drop down using any code. But still you can change the selected option using the code. Pls see this

http://www.mredkj.com/tutorials/tutorial003.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.