I have a div
element used as a toggle, and a select
element which shall be shown/hidden in the way that:
(1) When clicking the toggle, the select
shall be shown and focused, if it was hidden
(2) When clicking the toggle, the select
shall be hidden, if it was visible
(3) When the select looses focus, it shall be hidden.
Now there is this edge case when the select
has focus and user clicks the toggle -- then both events, onclick
and onblur
, are fired and cancel each other, at least in Firefox:
<div id="toggle">toggle</div>
<div>foobar</div>
<select id="select" multiple>
<option>foo</option>
<option>bar</option>
</select>
document.getElementById('toggle').onclick = function () {
var select = document.getElementById('select');
select.style.display = select.style.display === 'none' ? 'inline' : 'none';
select.focus();
};
document.getElementById('select').onblur = function () {
this.style.display = 'none';
};
https://jsfiddle.net/2wrxykpd/
I tried checking event.explicitOriginalTarget
in the onblur
function (online hide select
if target of the onblur
event is not the toggle), which would work in Firefox, but not in Chrome.
So what would be the proper way to do this?