0

Is there any way to open a dropdown on clicking some other button?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ramya Ramachandran
  • 1,033
  • 1
  • 8
  • 13

1 Answers1

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