Tag: Modals

  • Lesson 29: Adding Interactive Components (Forms, Modals, Sliders) in Bootstrap

    📌 Objective: Learn how to add interactive UI components like forms, modals, and sliders to enhance user experience.


    🔹 1. Why Add Interactive Components?

    Interactive components make a website more engaging and user-friendly.

    💡 Key Benefits:
    ✔️ Enhances user experience – Forms for data collection, modals for popups.
    ✔️ Reduces page reloads – Users can interact without navigating away.
    ✔️ Improves engagement – Sliders & animations keep users interested.


    🔹 2. Adding Forms for User Input

    Forms allow users to submit data like contact information or feedback.

    1️⃣ Creating a Basic Bootstrap Form

    Example: Contact Form

    html
    <section class="container mt-5">
    <h2>Contact Us</h2>
    <form>
    <div class="mb-3">
    <label class="form-label">Your Name</label>
    <input type="text" class="form-control" placeholder="Enter your name">
    </div>
    <div class="mb-3">
    <label class="form-label">Your Email</label>
    <input type="email" class="form-control" placeholder="Enter your email">
    </div>
    <div class="mb-3">
    <label class="form-label">Message</label>
    <textarea class="form-control" rows="4" placeholder="Enter your message"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Send Message</button>
    </form>
    </section>

    🔹 What happens?

    • The form collects user data and provides a submit button.

    2️⃣ Form Validation & Error Handling

    Example: Bootstrap Form with Validation

    html
    <form class="needs-validation" novalidate>
    <div class="mb-3">
    <label class="form-label">Email</label>
    <input type="email" class="form-control" required>
    <div class="invalid-feedback">Please enter a valid email.</div>
    </div>
    <button type="submit" class="btn btn-success">Submit</button>
    </form>

    <script>
    document.querySelector("form").addEventListener("submit", function(event) {
    if (!this.checkValidity()) {
    event.preventDefault();
    this.classList.add("was-validated");
    }
    });
    </script>

    🔹 What happens?

    • If the email field is empty, an error message appears.

    🔹 3. Adding Bootstrap Modals (Popups)

    Modals are pop-up dialogs used for alerts, forms, or additional content.

    1️⃣ Creating a Simple Modal

    Example: Bootstrap Modal

    html
    <!-- Button to Open Modal -->
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
    Open Modal
    </button>

    <!-- Modal Structure -->
    <div class="modal fade" id="myModal">
    <div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title">My Bootstrap Modal</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
    </div>
    <div class="modal-body">
    This is a simple Bootstrap modal!
    </div>
    </div>
    </div>
    </div>

    🔹 What happens?

    • Clicking the button triggers the modal popup.

    2️⃣ Using Modals for Forms

    Example: Contact Form Inside a Modal

    html
    <!-- Button to Open Modal -->
    <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#contactModal">
    Contact Us
    </button>

    <!-- Modal -->
    <div class="modal fade" id="contactModal">
    <div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title">Contact Us</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
    </div>
    <div class="modal-body">
    <form>
    <div class="mb-3">
    <label class="form-label">Your Name</label>
    <input type="text" class="form-control" placeholder="Enter your name">
    </div>
    <div class="mb-3">
    <label class="form-label">Your Email</label>
    <input type="email" class="form-control" placeholder="Enter your email">
    </div>
    <button type="submit" class="btn btn-primary">Send</button>
    </form>
    </div>
    </div>
    </div>
    </div>

    🔹 What happens?

    • The modal contains a contact form, improving user experience.

    🔹 4. Adding Bootstrap Sliders (Carousels)

    A slider (carousel) rotates images or content automatically.

    1️⃣ Basic Image Carousel

    Example: Bootstrap Image Slider

    html
    <div id="imageCarousel" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-inner">
    <div class="carousel-item active">
    <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 1">
    </div>
    <div class="carousel-item">
    <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 2">
    </div>
    </div>
    <button class="carousel-control-prev" type="button" data-bs-target="#imageCarousel" data-bs-slide="prev">
    <span class="carousel-control-prev-icon"></span>
    </button>
    <button class="carousel-control-next" type="button" data-bs-target="#imageCarousel" data-bs-slide="next">
    <span class="carousel-control-next-icon"></span>
    </button>
    </div>

    🔹 What happens?

    • The slider cycles through images automatically.
    • Navigation buttons allow users to move between slides.

    2️⃣ Adding Captions & Indicators

    Example: Slider with Captions

    html
    <div id="captionCarousel" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-indicators">
    <button type="button" data-bs-target="#captionCarousel" data-bs-slide-to="0" class="active"></button>
    <button type="button" data-bs-target="#captionCarousel" data-bs-slide-to="1"></button>
    </div>

    <div class="carousel-inner">
    <div class="carousel-item active">
    <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 1">
    <div class="carousel-caption">
    <h5>First Slide</h5>
    <p>Example caption for Slide 1</p>
    </div>
    </div>
    <div class="carousel-item">
    <img src="https://via.placeholder.com/800x400" class="d-block w-100" alt="Slide 2">
    <div class="carousel-caption">
    <h5>Second Slide</h5>
    <p>Example caption for Slide 2</p>
    </div>
    </div>
    </div>
    </div>

    🔹 What happens?

    • Each slide has captions and navigation indicators.

    📝 Lesson Recap Quiz

    🎯 Test your knowledge of Bootstrap interactive components.

    📌 Multiple-Choice Questions (MCQs)

    1️⃣ What Bootstrap component is used for popups?
    a) .popup-box
    b) .modal
    c) .alert
    d) .tooltip

    Correct Answer: b) .modal


    2️⃣ What Bootstrap class adds form validation?
    a) .form-check
    b) .form-validate
    c) .was-validated
    d) .needs-validation

    Correct Answer: d) .needs-validation


    🎯 Practical Challenge

    ✅ Build a contact form with validation.
    ✅ Create a modal popup with a form inside.
    ✅ Add a Bootstrap slider with captions and navigation buttons.
    ✅ Post your solution in the discussion forum for feedback!