Using Javascript, let say I have 3 container background that I can't access with, inside that container I have a button that do something, ex: open a new window, alert and modal.
If I want to click the background and target to that button to do the same thing in the same container, how to do that?
I tried using target or current target, but doesn't work.
const linkButton = document.querySelector(".link");
const alertButton = document.querySelector(".alert");
const backgroundClick = document.querySelector(".background");
linkButton.addEventListener("click", function () {
console.log("test");
})
alertButton.addEventListener("click", function () {
alert("alert");
})
backgroundClick.addEventListener("click", function () {
console.log("working");
})
body {
color: #eaeaea;
padding: 0;
margin: 0;
font: 16px "Open Sans", Helvetica, Arial, sans-serif;
}
.background {
background: yellow;
padding: 50px;
text-align: center;
margin-bottom: 20px;
}
/* The button used to close the modal */
.closebtn {
text-decoration: none;
float: right;
font-size: 35px;
font-weight: bold;
color: #fff;
}
.closebtn:hover,
.closebtn:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
header {
background-color: #5cb85c;
font-size: 25px;
color: white;
width: 50%;
margin: auto;
}
/*Open modal*/
/* The modal's background */
.modal {
display: none;
position: fixed;
left: 0;
top: 0;
width: 100%;
margin: auto;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.4);
}
/* Display the modal when targeted */
.modal:target {
display: table;
position: absolute;
}
/* The modal box */
.modal-dialog {
display: table-cell;
vertical-align: middle;
}
<div class="background">
<a class="link" href="https://www.google.com/" target="_" style="display: inline-block;">
<button>Button Link</button>
</a>
</div>
<div class="background">
<button class="alert">Button Alert</button>
</div>
<div class="background">
<a href="#id01">
<Open class="alert">Open Modal</button>
</a>
<div id="id01" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<header class="container">
<a href="#" class="closebtn">×</a>
<h2>Modal Header</h2>
</header>
</div>
</div>
</div>
</div>