Home > Web Front-end > JS Tutorial > How to Pass Data to a Bootstrap Modal When Clicking a Hyperlink?

How to Pass Data to a Bootstrap Modal When Clicking a Hyperlink?

Susan Sarandon
Release: 2024-12-26 02:55:13
Original
292 people have browsed it

How to Pass Data to a Bootstrap Modal When Clicking a Hyperlink?

Passing Data to a Bootstrap Modal

The task at hand involves opening a Bootstrap modal upon clicking a hyperlink and passing an associated ID to the modal. Despite attempts, users encounter an issue where the modal remains inactive or lacks the necessary data.

Solution

Leveraging the jQuery framework's .on event handler proves to be an effective method. Consider the following approach:

HTML

<a data-toggle="modal" data-id="ISBN564541" title="Add this item">
Copy after login

JavaScript

$(document).on("click", ".open-AddBookDialog", function () {
    var myBookId = $(this).data('id'); // Retrieve the ISBN from the clicked link
    $(".modal-body #bookId").val(myBookId); // Set the input field's value in the modal
    // It is unnecessary to manually call the modal as Bootstrap handles it with data-toggle="modal"

});
Copy after login

With this approach, clicking the hyperlink will both open the modal and populate the input field with the ID retrieved from the link. This solution effectively addresses the issue of passing data to the Bootstrap modal upon clicking a hyperlink.

The above is the detailed content of How to Pass Data to a Bootstrap Modal When Clicking a Hyperlink?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template