My goal is :
- to show a popin when the user clicks on a button
- to hide the popin when the user clicks elsewhere
I've tried this solution, and it works great : Use jQuery to hide a DIV when the user clicks outside of it
But I wanted to try something differently, I wanted to try focus()
So I made this code :
div
{
width: 100px;
height: 100px;
border: 1px solid blue;
}
div:focus
{
background-color: red;
}
<script>
function show()
{
document.getElementById("element").focus();
}
</script>
<input type="button" onclick="show()" value="click me!"/>
<div id="element"tabindex="-1">
</div>
After you clicked the button, click elsewhere to remove the red background
And it works great, now I want to hide the block and show it when the user clicks the button, I just added display: block
and display: none
, that's the only difference between the two versions
div
{
width: 100px;
height: 100px;
border: 1px solid blue;
display: none;
}
div:focus
{
background-color: red;
display: block;
}
<script>
function show()
{
document.getElementById("element").focus();
}
</script>
<input type="button" onclick="show()" value="click me!"/>
<div id="element"tabindex="-1">
</div>
And it's not working anymore, can someone tell me why?