I want my website visitors to interact with a button in order to trigger the opening of a modal that I have custom created using JavaScript. The process involved copying and pasting CSS, HTML, and JS into a single HTML document, which was then uploaded to the header of my website using a WordPress plugin called WP Code Snippets.
Within the specific WordPress theme I am using, there is an option to select a button and assign it as a trigger for a JavaScript pop-up window. How can I create an "onclick action link" for this button using the code that I have developed?
All of the code has been put together in a somewhat makeshift manner, but the end result is a functional modal. Now, the question remains - how do I convert it into an actionable link?
See below for the code:
<!DOCTYPE html>
<html>
<body>
<section class="modal hidden">
<div class="flex">
<button class="btn-close">⨉</button>
</div>
<input type=button onClick="location.href='https:www.login.com'" value='Current Clients'>
<input type=button onClick="location.href='https://www.bookhere.com'" value='New Clients'>
</section>
<div class="overlay hidden"></div>
<button class="btn btn-open">Open Modal</button>
</body>
</html>
<script>
const modal = document.querySelector(".modal");
const overlay = document.querySelector(".overlay");
const openModalBtn = document.querySelector(".btn-open");
const closeModalBtn = document.querySelector(".btn-close");
// close modal function
const closeModal = function() {
modal.classList.add("hidden");
overlay.classList.add("hidden");
};
// close the modal when the close button and overlay is clicked
closeModalBtn.addEventListener("click", closeModal);
overlay.addEventListener("click", closeModal);
// close modal when the Esc key is pressed
document.addEventListener("keydown", function(e) {
if (e.key === "Escape" && !modal.classList.contains("hidden")) {
closeModal();
}
});
// open modal function
const openModal = function() {
modal.classList.remove("hidden");
overlay.classList.remove("hidden");
};
// open modal event
openModalBtn.addEventListener("click", openModal);
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Inter", sans-serif;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #222;
position: relative;
min-height: 100vh;
background-color: #b3e6f4;
}
.modal {
display: flex;
flex-direction: column;
justify-content: center;
gap: 0.4rem;
width: 450px;
padding: 1.3rem;
min-height: 250px;
position: absolute;
z-index: 2;
top: 20%;
background-color: #c8b490;
border: 1px solid #c8b490;
border-radius: 15px;
}
.modal .flex {
display: flex;
align-items: center;
justify-content: space-between;
}
.modal input {
padding: 0.7rem 1rem;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 1.0em;
font-family: georgia;
font-weight: 500;
background-color: #e8e4db;
}
.modal input:hover {
background-color: #e8cfaa;
font-color: #e8e4db;
}
.modal p {
font-size: 1.2rem;
color: #777;
margin: 0.4rem 0 0.2rem;
}
button {
cursor: pointer;
border: none;
font-weight: 600;
background-color: #625d5a
}
.btn {
display: inline-block;
padding: 0.8rem 1.4rem;
font-weight: 700;
background-color: black;
color: white;
border-radius: 5px;
text-align: center;
font-size: 1em;
}
.btn-open {
position: absolute;
bottom: 150px;
}
.btn-close {
transform: translate(10px, -20px);
padding: 0.5rem 0.7rem;
background: #eee;
border-radius: 50%;
}
.overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(3px);
z-index: 1;
}
.hidden {
display: none;
}
</style>