I have a basic question that I think somebody may easily be able to answer. I have very little coding experience but just need one simple change to complete website I am making,.
I have a simple toggle similar to this example I have used. :
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top: 20px;
}
</style>
</head>
<body>
<text onclick="myFunction()">Try it</text>
<div id="myDIV">
This is my DIV element.
</div>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
</body>
</html>
I would like the #myDIV
to disappear when clicking outside the div/container as well as disappear when clicking on the "Try it" text/button. I guess I must edit the script. I have tried for a few hours but no luck.
Could somebody help me with this? Sorry if silly / basic question.