I’m trying to make a CSS & JS based dropdown-menu.
This example works in all browsers:
However, I want to make sure the item loses focus and automatically closes after clicking on something else in the <body>. I’m trying to accomplish that with the following code:
Now, the .blur function does work in all the browsers, but there’s no other text inserted anymore and when I click twice on the element, it closes and opens again, instead of just closing.
I’m lost here. I want to keep the .blur function of course, but I want to make sure normal behavior can be kept.
Any help is much appreciated!
To hide it again, bind a click listener to the document, then you can check if the thing you’ve clicked on is in your drop down and return false if it is, or hide if it’s not. Effectively that means if you click on anything other than a link in the drop down, it will hide it.
I’ve also updated your jsFiddle