To open a modal window, you should use data-toggle="modal" and href. If it's not <a>, you can use data-target instead:

data-toggle="modal" data-target="#exampleModal"

To pass a unique value to the modal, you need to use data-* attributes. This can be given to the modal handler like this:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>

The next thing is that, you need to handle the custom input. Once the modal window is shown, you need to execute some stuff. For that, you need to assign an event handler, once the modal window is shown:

// Execute something when the modal window is shown.
$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  var recipient = button.data('whatever'); // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this);
  modal.find('.modal-title').text('New message to ' + recipient);
  modal.find('.modal-body input').val(recipient);
});

The issues with your code:

  1. Assigning multiple elements with same id is a crime. Do not reuse id as they are unique.
  2. You need to assign the data-toggle to the <a> tag.
  3. You should pass the attributes through data-* attributes.

Working Snippet

$(function () {
  $('#myModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var code = button.data('code'); // Extract info from data-* attributes
    var company = button.data('company'); // Extract info from data-* attributes
    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this);
    modal.find('#code').val(code);
    modal.find('#company').val(company);
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<a href="#myModal" class="btn btn-info btn-lg" data-toggle="modal" data-code="code" data-company="company name">
  <img src="../images/edit.png" style="width:20px;">
</a>

<div class="modal fade bs-example-modal-sm" tabindex="-1" id="myModal">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" id="mySmallModalLabel">Codes &amp; Company</h4>
      </div>
      <div class="modal-body">
        <input type="text" id="code" readonly />
        <input type="text" id="company" readonly />
      </div>
    </div>
  </div>
</div>

Please map your code with the above code. You don't need to change any other values. Just the way <span> is done.

Answer from Praveen Kumar Purushothaman on Stack Overflow
Top answer
1 of 1
5

To open a modal window, you should use data-toggle="modal" and href. If it's not <a>, you can use data-target instead:

data-toggle="modal" data-target="#exampleModal"

To pass a unique value to the modal, you need to use data-* attributes. This can be given to the modal handler like this:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>

The next thing is that, you need to handle the custom input. Once the modal window is shown, you need to execute some stuff. For that, you need to assign an event handler, once the modal window is shown:

// Execute something when the modal window is shown.
$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  var recipient = button.data('whatever'); // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this);
  modal.find('.modal-title').text('New message to ' + recipient);
  modal.find('.modal-body input').val(recipient);
});

The issues with your code:

  1. Assigning multiple elements with same id is a crime. Do not reuse id as they are unique.
  2. You need to assign the data-toggle to the <a> tag.
  3. You should pass the attributes through data-* attributes.

Working Snippet

$(function () {
  $('#myModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var code = button.data('code'); // Extract info from data-* attributes
    var company = button.data('company'); // Extract info from data-* attributes
    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this);
    modal.find('#code').val(code);
    modal.find('#company').val(company);
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<a href="#myModal" class="btn btn-info btn-lg" data-toggle="modal" data-code="code" data-company="company name">
  <img src="../images/edit.png" style="width:20px;">
</a>

<div class="modal fade bs-example-modal-sm" tabindex="-1" id="myModal">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" id="mySmallModalLabel">Codes &amp; Company</h4>
      </div>
      <div class="modal-body">
        <input type="text" id="code" readonly />
        <input type="text" id="company" readonly />
      </div>
    </div>
  </div>
</div>

Please map your code with the above code. You don't need to change any other values. Just the way <span> is done.

🌐
Appsloveworld
appsloveworld.com › php › 593 › onclick-pass-value-to-popup-modal
Onclick pass value to popup modal - appsloveworld.com
To open a modal window, you should use data-toggle="modal" and href. If it's not <a>, you can use data-target instead: ... To pass a unique value to the modal, you need to use data-* attributes.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-pass-data-into-a-bootstrap-modal
How to Pass Data into a Bootstrap Modal? - GeeksforGeeks
To pass data into the modal body jQuery methods are used. In this approach, the web page consists of two input fields Name and Marks which accept input from the user. The data entered in the input fields are extracted using the IDs of the respective ...
Published   July 29, 2024
🌐
W3Schools
w3schools.invisionzone.com › browser scripting › javascript
Passing parameters to modal dialog - W3Schools Forum
March 24, 2017 - I am using the W3Schools CSS/JS modal dialog example: https://www.w3schools.com/howto/howto_css_modals.asp However I would like to be able to pass a parameter from the button to the modal window as a variable so I can use it to perform a database query and populate the modal. I HAVE looked throug...
Top answer
1 of 1
4

Instead of using an input field you could create a container element for the id, a div or span and update content of the element using .html() jQuery method.

$(document).on("click", ".open-homeEvents", function () {
     var eventId = $(this).data('id');
     $('#idHolder').html( eventId );
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Modal</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />	
</head>
<body>
<button class="open-homeEvents btn btn-primary" data-id="2014-123456"  data-toggle="modal" data-target="#modalHomeEvents">More Details</button>	
<div id="modalHomeEvents" class="modal fade" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header" style="height:50px;">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">

         <label>Student ID</label>     
        <input type="hidden" name="eventId" id="eventId"/>
        	<span id="idHolder"></span>	
        </div>
        <div class="modal-footer">
          <input type="submit" class="btn btn-primary" value="Login" name="login" style="background-color:rgb(0,30,66); ">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
        </div>
      </div>

    </div>
  </div>	
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

🌐
Microsoft
social.msdn.microsoft.com › Forums › en-US › 54c2eae9-8a83-4408-b672-c24b90cb318f › how-to-pass-variable-value-to-modal-in-javascript
How to pass variable value to Modal in Javascript | Microsoft Learn
October 5, 2018 - In class btn btn-danger value is ok. But in deleteconfirmModal it shows undefined · var Id; $(document).ready(function () { $('.btn.btn-danger').on('click', function () { var Id = $(this).data('id'); $('#deleteConfirmModal').modal('show'); }); $("#deleteConfirmModal").on('click', "#deleteConfirm", function () { debugger; console.log(Id); return false; }); });
🌐
Archlinux
copyprogramming.com › t › onclick-pass-value-to-popup-modal
Onclick Pass Value To Popup Modal - CopyProgramming
Solution: You don't really need to "pass data, .onclick = function() { modal2.textContent = 'different text!', > Solution 2: The best way is to pass, () { return row; } } }); }; And then pass, "> <button class="btn btn-success" id="change" onclick="func(this)" name="change"
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 62690181 › onclick-pass-value-to-popup-modal-laravel-jquery
onclick pass value to popup modal laravel jquery
July 2, 2020 - i'm trying to get data-staffId value in modal popup but on show.bs.modal event not working: $(function(){ $('#roasterManagementShifts').on('show.bs.modal',function(e){ var button = $(event.relatedTarget); var staffId = button.data('staffId') var model =$(this); var aro = modal.find('#staffId').val('staffId'); console.log(aro); }); }); Is there any better way to pass value to modal popup?
🌐
Stack Overflow
stackoverflow.com › questions › 41728926 › pass-data-to-modal-popup
Pass data to modal popup - javascript
You don't really need to "pass data" to the modal -- it's part of the window, and you can use Javascript to affect it in the same way you do the button. For example, if you wanted to change the text in the modal, you could just do: btn3.onclick ...
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 538554 › how-to-pass-value-to-modal-popup-from-linkbutton-in-asp-net
How to pass value to modal popup from linkbutton in asp.net
September 4, 2022 - The original script has a syntax error and uses .val() on a LinkButton (anchors do not expose .val()), selecting #linkbutton1 won't reliably match buttons generated inside a GridView, and CommandArgument is a server-side property (not emitted as a client data-attribute). @Dani was right to point toward the modal show event, but the handler needs to read the element that actually triggered the modal.
Top answer
1 of 1
1

When we display post data in model with on click of button we have to provide the post-id with data attribute in for specific model target.

Please add post-id in data-target in anchor tag as well as in main div tag ID.

I have modified your code here:

<div><?php echo wp_get_attachment_image($image_upload,$size = 'full');?><br/>
  <span class="name"><?php echo $title; ?></span><span class="jobtitle"><?php echo $jobtitle; ?></span>
  <a onclick="return moreinfoModal(this);" id="<?php echo get_the_ID();?>" data-toggle="modal" data-target="#moreinfo-modal-<?php echo get_the_ID();?>" href="javascript:void(0);" class="info">more info</a>
</div>


<div id="moreinfo-modal-<?php echo get_the_ID();?>" class= "moreinfo-modal" role="dialog">
      <div class="teammember_div">
        <a id="close-moreinfo" onclick='return closeMoreInfoModal();' href="javascript:void(0);"><img src="https://brentbrookbush.com/wp-content/themes/brent/images/svgs/icon.svg"></a>
        <?php echo wp_get_attachment_image($image_upload,$size = 'full');?>
        <p><?php echo $title ?></p>
        <p><?php echo $about ?></p>
        <span class="jobtitle"><?php echo $jobtitle ?></span>

        <span id="moreinfo" style="display: inherit;"></span>
        <a id="modal-join" class="btn-cta" href="/sign-up/">Become a Member!</a>
      </div>
    </div>

<?php }?>
</div>
</div>

As well as you can change js code with click on class instead of div ID attribut like:

<script> function moreinfoModal(field) {
        console.log(field.id);
        $('.moreinfo-modal').toggleClass('open');
    }


    function closeMoreInfoModal() {
        $('.moreinfo-modal').toggleClass('open');
    }

    $(document).on('click', '.close-pill', function(e) {
        $(this).parent().remove();
        e.preventDefault();
    });
    </script>

Hope this can solved your problem. :)

🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 929002 › asp-route-onclick-help
asp-route onclick help - Microsoft Q&A
So, to add additional data to the <a> tag, you can use custom attribute, like data-{value}. Then, in the hyperlink click event, find the custom attribute and append the value at the end of the request url: Then, the output as below: view the source code from here: 222976-sourcecode.txt · Finally, in the Ajax success function, you can show the popup modal.
🌐
Team Treehouse
teamtreehouse.com › community › how-to-pass-a-variable-to-a-modal-from-the-button
How to pass a variable to a modal from the button. (Example) | Treehouse Community
June 10, 2017 - "'>\n"; $form .= "<input type='submit' class='btn btn-info' name='submitnew' value='Add Debt'>\n"; $form .= "</form></td>\n"; $form .= "</tr>\n"; $form .= "</table>\n"; $form .= "<!-- Trigger the modal with a button -->"; $form .= "<!-- Modal -->"; $form .= "<div id='myHistory' class='modal fade' role='dialog'>"; $form .= " <div class='modal-dialog modal-lg'>"; $form .= " <!-- Modal content-->"; $form .= " <div class='modal-content'>"; $form .= " <div class='modal-header'>"; $form .= " <button type='button' class='close' data-dismiss='modal'>&times;</button>"; $form .= " <h4 class='modal-title
🌐
GitHub
github.com › mpontus › react-modal-hook › issues › 2
Passing props to modal dialog · Issue #2 · mpontus/react-modal-hook
December 24, 2018 - let props = {}; export function setMyDialogProps(newprops) { props = newprops; } export default function MyDialog() { return ( <div role="dialog" className="modal"> <div className="modal-content"> <p>Modal content</p> <button onClick={props.hideDialog}>Hide modal</button> </div> </div> ); }
Author   tgochenour
🌐
Flowbite React
flowbite-react.com › docs › components › modal
React Modal - Flowbite
</p> </div> </ModalBody> <ModalFooter> <Button onClick={() => setOpenModal(false)}>I accept</Button> <Button color="alternative" onClick={() => setOpenModal(false)}> Decline </Button> </ModalFooter> </Modal> </> ); }Expand code · Use this example by passing the popup prop from React to the modal component to show a dialog to the user asking for a decision such as when confirming an item deletion from the database.
🌐
Experts Exchange
experts-exchange.com › questions › 28703133 › Declare-value-of-variable-and-pass-it-to-form-value-modal-window-jquery.html
Solved: Declare value of variable and pass it to form value. (modal window/jquery) | Experts Exchange
August 5, 2015 - You have to manually populate the ... what you have done for the idfield - retrieve the values you need to populate and then set the corresponding field values (by id) in the modal form....
🌐
DevDreamz
devdreamz.com › question › 183474-onclick-pass-value-to-popup-modal
Onclick pass value to popup modal - DevDreamz
We'll use jQuery here, but you ... with same id is a crime. Do not reuse id as they are unique. You need to assign the data-toggle to the <a> tag. You should pass the attributes through data-* attributes....