I\'ve got a couple of hyperlinks that each have an ID attached. When I click on this link, I want to open a modal ( http://twitter.github.com/bootstrap/javascript.html#modal
Bootstrap 4.0 gives an option to modify modal data using jquery: https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content
Here is the script tag on the docs :
$('#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)
})
It works for the most part. Only call to modal was not working. Here is a modification on the script that works for me:
$(document).on('show.bs.modal', '#exampleModal',function(event){
... // same as in above script
})
If you are on bootstrap 3+, this can be shortened a bit further if using Jquery.
HTML
<a href="#" data-target="#my_modal" data-toggle="modal" class="identifyingClass" data-id="my_id_value">Open Modal</a>
<div class="modal fade" id="my_modal" tabindex="-1" role="dialog" aria-labelledby="my_modalLabel">
<div class="modal-dialog" role="dialog">
<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="myModalLabel">Modal Title</h4>
</div>
<div class="modal-body">
Modal Body
<input type="hidden" name="hiddenValue" id="hiddenValue" value="" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
JQUERY
<script type="text/javascript">
$(function () {
$(".identifyingClass").click(function () {
var my_id_value = $(this).data('id');
$(".modal-body #hiddenValue").val(my_id_value);
})
});
</script>
Try with this
$(function(){
//when click a button
$("#miButton").click(function(){
$(".dynamic-field").remove();
//pass the data in the modal body adding html elements
$('#myModal .modal-body').html('<input type="hidden" name="name" value="your value" class="dynamic-field">') ;
//open the modal
$('#myModal').modal('show')
})
})
I think you can make this work using jQuery's .on event handler.
Here's a fiddle you can test; just make sure to expand the HTML frame in the fiddle as much as possible so you can view the modal.
http://jsfiddle.net/Au9tc/605/
HTML
<p>Link 1</p>
<a data-toggle="modal" data-id="ISBN564541" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<p> </p>
<p>Link 2</p>
<a data-toggle="modal" data-id="ISBN-001122" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<div class="modal hide" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>some content</p>
<input type="text" name="bookId" id="bookId" value=""/>
</div>
</div>
JAVASCRIPT
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #bookId").val( myBookId );
// As pointed out in comments,
// it is unnecessary to have to manually call the modal.
// $('#addBookDialog').modal('show');
});
This is so easy with jquery:
If below is your anchor link:
<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>
In the show event of your modal you can access to the anchor tag like below
//triggered when modal is shown
$('#modal_id').on('shown.bs.modal', function(event) {
// The reference tag is your anchor tag here
var reference_tag = $(event.relatedTarget);
var id = reference_tag.data('id')
// ...
// ...
})
This is how you can send the id_data to a modal :
<input
href="#"
data-some-id="uid0123456789"
data-toggle="modal"
data-target="#my_modal"
value="SHOW MODAL"
type="submit"
class="btn modal-btn"/>
<div class="col-md-5">
<div class="modal fade" id="my_modal">
<div class="modal-body modal-content">
<h2 name="hiddenValue" id="hiddenValue" />
</div>
<div class="modal-footer" />
</div>
And the javascript :
$(function () {
$(".modal-btn").click(function (){
var data_var = $(this).data('some-id');
$(".modal-body h2").text(data_var);
})
});