0

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

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Ramya Ramachandran
  • 723
  • 1
  • 5
  • 11

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
  • 16,498
  • 7
  • 57
  • 66