Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript + addEventListener 'click' ignored on select html object

Tags:

javascript

I've added an eventListener on the document in hopes to hide myWidget whenever anything within the document is clicked (except for the widget of course).

myWidget.addEventListener('click', function (e) {
    e.stopPropagation(); 
}, false);

document.addEventListener('click', function (e) {
    myWidget.style.display = 'none';
    console.log(e.target);
});

Now this seems to work on everything but select html objects. Selecting a select box shows the select content, but the widget is not hidden and the console.log never fires. Any reason why?

like image 583
worked Avatar asked Feb 14 '26 13:02

worked


1 Answers

I think you want the change event

document.addEventListener('change', function (e) {
    myWidget.style.display = 'none';
    console.log(e.target);
});
like image 133
Adam Rackis Avatar answered Feb 17 '26 02:02

Adam Rackis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!