DiscoverWeekly Code QuickiesCreate a Message Pop-Up Modal with JavaScript and Tailwind CSS
Create a Message Pop-Up Modal with JavaScript and Tailwind CSS

Create a Message Pop-Up Modal with JavaScript and Tailwind CSS

Update: 2025-04-29
Share

Description

In this project, we will build a message pop-up modal using JavaScript and Tailwind CSS. This project is part of my "Learn Web Development From the Beginning" series, where we progressively create real-world projects with HTML, CSS, JavaScript, and Tailwind.

πŸ”₯ Become a channel member and get full access to all my web development courses: https://www.youtube.com/playlist?list=UUMOx8iaEliW-CFrS0t9KHisTw

πŸ“šGET ULTIMATE COURSES BUNDLES: https://norbert-bm-s-school.teachable.com/

You’ll learn how to:

* Create a hidden modal

* Toggle modal visibility with JavaScript

* Send and validate a message input

* Close the modal when clicking outside or on a cancel button

Step 1: Set Up the Basic Project Structure

* Create a new folder for your project.

* Inside, create:

* index.html

* style.css (optional if you want custom styles)

* script.js

* Link TailwindCSS via CDN in your HTML file.






Message Modal






Step 2: Build the Modal Structure (HTML)

Create the modal structure inside the . Initially, the modal should be hidden.


<button class="bg-blue-600 text-white p-2 rounded hover:bg-blue-700" id="message-button">
Open Message Modal
</button>




Send a Message


<textarea class="mt-4 w-full h-32 border rounded p-2" id="modal-input"></textarea>

<button class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700" id="send-button">Send</button>
<button class="bg-gray-400 text-white px-4 py-2 rounded hover:bg-gray-500" id="close-button">Cancel</button>



Step 3: Handle the Modal with JavaScript

In script.js, write the logic to open, close, and send messages.

// Select Elements
const modal = document.getElementById('modal');
const messageButton = document.getElementById('message-button');
const sendButton = document.getElementById('send-button');
const closeButton = document.getElementById('close-button');
const messageInput = document.getElementById('modal-input');

// Toggle Modal Visibility
function toggleModal() {
modal.classList.toggle('hidden');
}

// Open Modal
messageButton.addEventListener('click', toggleModal);

// Close Modal on Cancel
closeButton.addEventListener('click', toggleModal);

// Close Modal When Clicking Outside
modal.addEventListener('click', function (event) {
if (event.target === modal) {
toggleModal();
}
});

// Send Message
sendButton.addEventListener('click', function () {
if (messageInput.value.trim() === '') {
alert('Please enter a message.');
return;
}
alert(`Message sent: ${messageInput.value}`);
messageInput.value = ''; // Clear input
toggleModal(); // Close modal
});

Step 4: (Optional) Enable Dark Mode Toggle

Add a dark mode toggle button if you want to enable Tailwind’s dark mode classes dynamically.

Final Result

* Click the "Open Message Modal" button βž” Modal opens.

* Type a message and click Send βž” Shows an alert and closes the modal.

* Click Cancel or click outside the modal βž” Closes the modal.

Congratulations! πŸŽ‰ You now have a fully functioning, stylish message modal!

Complete Source Code: https://github.com/NorbertBM/learn-web-development-for-beginners-.git



Get full access to Norbert’s Web Dev School at norbertbmwebdev.substack.com/subscribe
CommentsΒ 
In Channel
How to learn anything

How to learn anything

2025-03-3103:32

loading
00:00
00:00
x

0.5x

0.8x

1.0x

1.25x

1.5x

2.0x

3.0x

Sleep Timer

Off

End of Episode

5 Minutes

10 Minutes

15 Minutes

30 Minutes

45 Minutes

60 Minutes

120 Minutes

Create a Message Pop-Up Modal with JavaScript and Tailwind CSS

Create a Message Pop-Up Modal with JavaScript and Tailwind CSS

Norbert B.M.