Is there any way to open a dropdown on clicking some other button?
Asked
Active
Viewed 1,042 times
0

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Ramya Ramachandran
- 1,033
- 1
- 8
- 13
-
When binding the click even, you can just trigger the dropdown of another element in your event handler. – Douglas Gaskell Apr 26 '16 at 01:51
1 Answers
2
Here you Go!!
JS
function VM() {
var message = ko.observable("hello!!");
function onButtonClick() {
var dropdown = document.getElementById('dropdown');
showDropDown(dropdown);
}
function showDropDown(element){
var event;
event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown', true, true, window);
element.dispatchEvent(event);
}
return {
message: message,
onButtonClick:onButtonClick
}
}
ko.applyBindings(new VM());
HTML
<button data-bind="click: onButtonClick">
Button
</button>
<select id="dropdown" >
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
Here's JSFiddle! with the solution
Updated the solution from How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)? for Knockout

Community
- 1
- 1

Madhu Ranjan
- 17,334
- 7
- 60
- 69