sab
sab

Reputation: 5032

Event before the opening of a select. (click does not work fast enough)

I want to call a method when the user click on a select, just before the option-list appear. ( Click events are called after)

I want to do it in order to modify the option-list just before the user can see it.

Any idea?

Upvotes: 0

Views: 294

Answers (1)

j08691
j08691

Reputation: 207923

Both the mousedown and focus events would allow you to modify the options prior to opening the drop down list.

var el = document.getElementById("sel");

// Change focus to click or mousedown to see the difference
el.addEventListener("focus", modifyOpt, false);

function modifyOpt() {
  var opts = sel.options;
  opts[0].innerHTML = '2';
}
<select id="sel">
  <option>1</option>
</select>

Upvotes: 2

Related Questions