I\'m trying to call up a click element to open up a modal box, and i\'m unsure what I\'m doing wrong.
I\'ve done basic debugging, but I\'m still relatively new to this,
#teal {
height: 100px;
width: 100px;
background-color: red;
}
Block Level Elements like <div>
typically need to have their dimensions defined unless they are going to expand to their inner content. You need to define a height and width to the div inside the anchor tag in order to create a clickable area. Right now your button is essentially 0h x 0w.
Additional tip: you can get rid of the <div>
inside the anchor and just apply display:block
to it, as well as define the height and width. That will keep your HTML as simple as it needs to be.
That happens because click onto <a>
tag forces browser to follow provided link. So page reloads.
var focus = document.getElementById("teal");
var modal = document.getElementById("bg-modal");
if (focus) {
focus.addEventListener("click", function(event) {
event.preventDefault();
modal.style.display = "flex";
});
}
I also saw your duplicate question, where you provided <head>
tag.
Move javascript file import from top of the <head>
tag to bottom of the <body>
tag
You have a click handler on your <div id="teal" />
but it doesn't give the user any way to click it
Add this css snippet and you should be able to click the element
#teal {
height: 50px;
width: 50px;
background-color: blue;
}
I think your problem is this,
<a id = "teal-link" href = "">
<div id = 'teal'>
</div>
</a>
You have wrap your div with anchor
tag with href=""
. By pressing on div your page is getting refreshed. You need to do this,
<a id = "teal-link" href = "#"> //Add # here
<div id = 'teal'>
</div>
</a>
Demo