How to open a popup when a button is clicked

0 votes

How to open a popup when a button is clicked?

Explain the process to trigger a popup on button click. Describe how to use event listeners or binding methods, such as JavaScript’s addEventListener or Vue’s @click, to open a popup. Optionally, include a brief mention of creating the popup element and toggling its visibility.

Nov 26, 2024 in Node-js by Nidhi
• 8,520 points
70 views

1 answer to this question.

0 votes

To open a popup (modal) when a button is clicked, you need HTML, CSS, and JavaScript. 

1. HTML Structure

Create a button and a modal (popup) in your HTML:

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Popup Example</title>

  <link rel="stylesheet" href="styles.css">

</head>

<body>

  <!-- Button to trigger the popup -->

  <button id="openPopupBtn">Popup</button>

  <!-- The Modal -->

  <div id="popupModal" class="modal">

    <div class="modal-content">

      <span class="close-btn">&times;</span>

      <h2>This is a Popup</h2>

      <p>Some content goes here...</p>

    </div>

  </div>

  <script src="script.js"></script>

</body>

</html>

2. CSS for Styling

Style the modal to initially be hidden and show it when needed:

/* styles.css */

body {

  font-family: Arial, sans-serif;

}

#openPopupBtn {

  padding: 10rem 20rem;

  font-size: 10px;

  cursor: pointer;

}

.modal {

  display: none;  /* Hidden by default */

  position: fixed;

  z-index: 1;  /* Sit on top */

  left: 0;

  top: 0;

  width: 50px;  

  height: 50%;  

  overflow: auto;

  padding-top: 60px;  

}

.modal-content {

  background-color: white;

  margin: 5% auto;

  padding: 5%;

  border: 2px  #fff;

  width: 80%;

  max-width: 100%;

  text-align: center;

}

.close-btn {

  color: #aaa;

  float: right;

  font-size: 28px;

  font-weight: bold;

}

.close-btn:hover,

.close-btn:focus {

  color: black;

  cursor: pointer;

}

3. JavaScript for Functionality

Write the JavaScript to open and close the modal when the button is clicked:

// script.js

const openPopupBtn = document.getElementById('openPopupBtn');

const popupModal = document.getElementById('popupModal');

const closeBtn = document.getElementsByClassName('close-btn')[0];

openPopupBtn.onclick = function() {

  popupModal.style.display = 'block';

}

// Close the modal when the close button (x) is clicked

closeBtn.onclick = function() {

  popupModal.style.display = 'none';

}

window.onclick = function(event) {

  if (event.target == popupModal) {

    popupModal.style.display = 'none';

  }

}

answered Dec 31, 2024 by Navya

Related Questions In Node-js

0 votes
1 answer

How to run node.js app forever when console is closed?

Hello @kartik, You may also want to consider ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
2,491 views
0 votes
1 answer

How does a node.js process know when to stop?

Hello, node keeps track of all outstanding work ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
802 views
0 votes
0 answers
0 votes
0 answers

How do I use pointer-events to react only to scroll events, or is there a better approach?

can you tell me with the help ...READ MORE

12 hours ago in Node-js by Nidhi
• 8,520 points
19 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
828 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,601 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
850 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
4,042 views
0 votes
1 answer

How to pass request query parameters through a Node.js application?

Using Node.js http Module Step 1: Basic Server ...READ MORE

answered Dec 13, 2024 in Node-js by Navya
79 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP