#Open select input on Button click

8 messages · Page 1 of 1 (latest)

shut thorn
#

Is there any way to open a select element by clicking on a separate button?

lethal vigil
shut thorn
lethal vigil
shut thorn
#

In the source code, I see that opening occurs in the onMouseDown event. So the following code works form me:

selectRef.current.dispatchEvent(
    new MouseEvent("mousedown", { bubbles: true })
);

And to automatically close the select after clicking it, call focus() on input.
I replaced sandbox.

pure walrus
#

Hey @shut thorn - do you know how to close it programatically as well?

shut thorn
#

Maybe "mouseup" event?

pure walrus
#

Hmm, sounds like it could work - I'll try that out, thanks