Tips for connecting JavaScript to a specific onclick function script

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>

Answer №1

Perhaps you're looking for a toggle feature, where a function switches between opening and closing a modal window? In that case, simply create a new function named "toggleModal" that checks a boolean variable to determine whether it should trigger the openModal or closeModal functions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for passing state values into getServerSideProps to fetch data from an API

At the moment, I am dealing with a situation where I have a context containing the state of a name. My current task is to extract this state so that I can utilize it in an API call which relies on it. age.js import { useStateContext } from "@/context ...

Sending a form using an AngularJS dropdown menu

I have recently started using angularjs and decided to switch out a traditional html <select> box for an angular modal select box. The select box successfully populates with data from a $http ajax request, but I am facing issues with form submission ...

Experiencing a mysterious error in Vuex with undefined values while using a getter in a computed property

I've encountered an issue on my Vue.js website where the data is rendering correctly, but I keep receiving an undefined error in the console. The error seems to be related to an axios call I'm making in App.vue to fetch data from my CMS: In App. ...

Explore creating a dial number using React

Hey there, I'm currently working on developing a component. Just to clarify, I am not using react-native for this project. Instead, I would like to utilize React to scroll a number similar to how an image scrolls. https://i.sstatic.net/c1Tu8.png Th ...

Clicking on the child element triggers a blur event

I have a situation where a parent element contains an input: <div @blur="onLeaveSelect($event)"> <div v-if="searchActivated" > <input type="text" placeholder="Search..." @mousedown.stop> ...

Trigger ExtJS input file event when dialogue window is closed

How can we capture the event when a file is selected in an open dialogue box and the OK button is clicked in extjs? **Off topic, the field does not stretch from its normal width. xtype: 'textfield', fieldLabel: 'New (JPG or ...

Exploring the functionality of Async Storage in React Native for efficient data saving purposes

I'm completely new to working with Async and async storage. I am a bit lost and unsure of how to proceed, but my main objective is to use setWorkouts() to assign workouts based on the values I retrieve from asyncstorage, and then save these workouts. ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

Passing selection from child to parent in ReactJS

When it comes to passing data from parent components to child components, using props is a common practice. But how can data be sent back up to the parent component? I'm working on a set of dropdown menus where users can make selections: DropdownMen ...

Issues with Typescript when Adding Objects to an Array in a Function

In my Angular 2 application, I am encountering a problem when trying to add new items to an array. The issue appears to be related to Typescript types, although I'm not entirely certain. Here is the current structure of the array I am attempting to mo ...

"Exploring ways to implement validation for multiple email addresses in a textarea using JQuery or JavaScript, while allowing the addresses to be separated by semicol

NOTE: Each email ID must be unique. An empty string value is allowed to be added. Email IDs should be separated by semicolons and commas. Basic validation of email IDs is necessary for entry. [![ $("#d-notification").focusout(function () { var ...

Having issues installing Parcel through npm - Encountered an error while parsing package.json information

After creating a package.json file using the command npm init in my project folder, I proceeded to run npm i parcel --save-dev which resulted in an error message: C:\Users\XPRESS\Desktop\starter>npm i parcel --save-dev npm ERR! code ...

Having trouble displaying the Chrome context menu for a specific Chrome extension

I've read through several posts on this topic, but I'm still struggling to identify the issue with my implementation of the Chrome contextMenu API. I simply copied the code from a tutorial on Chrome APIs, and though there are no errors, the menu ...

JavaScript causing Axios network error

Recently, I've started exploring the combination of axios and stripe in my project but unfortunately, I have encountered some challenges. Whenever I attempt to initiate a post request using axios, an error pops up which looks like this: https://i.sta ...

The object's type remains a mystery

While working on implementing jwt authentication in Ionic, React with TypeScript, I faced a typescript error when trying to add a check in my App.tsx file after successful implementation. The error stated: Object is of type 'unknown' Below is ...

Creating a tailored regular expression for validating email addresses

Up to this point, I have crafted a regex pattern as Pattern="^(?=.*[a-zA-Z].*)([a-zA-Z0-9._%+-]+@([a-zA-Z0-9-]+[a-zA-Z0-9-]+(\.([a-zA-Z0-9-]+[a-zA-Z0-9-])+)?){1,4})$" that meets the following criteria: Not all characters should be numbers, ...

Launching an external software using electron

I am in the process of developing my own personalized Electron app for managing other applications on my device. One issue I have encountered is the inability to create a link that opens my own .exe applications. I have attempted various methods without su ...

Is there a way to detect a specific button press in react-native-picker-select?

I am currently utilizing the react-native-picker-select library. My objective is to set ingrebool to false when options a, b, c, or d are selected and true when option e is chosen from the data called ingre. How can I achieve this? Here is my code snippet ...

Safari is unable to display the button text, while Chrome has no problem showing it

In Safari, the text of the button is not showing up properly. I am able to retrieve the text using jQuery in the console though. However, there seems to be an issue with recognizing the click event. <div class="btn-group btn-group-lg"> <button ...

Create a self-bot that can generate a new server

I am currently working on a discord.js self-bot and I need assistance in creating a server. Any guidance would be greatly appreciated. I have experimented with the client.user method, but did not achieve the desired result. If it is not possible to c ...