Reputation: 37
I have a problem with jQuery. When I click on the link the modal opens and I want to pass the value of data-href
to the button inside the modal.
Nothing happens when I click the button. Could you please help me?
Link
<a href="#" data-href="stackoverflow.com" data-toggle="modal" data-target="#openModal">Link</a>
Modal
<div class="modal fade" id="openModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn btn-danger btn-ok" href="">Go</button>
</div>
</div>
</div>
</div>
JQuery
<script>
$('#openModal').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
$('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
});
</script>
jQuery/Popper/JS
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.2/umd/popper.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
Error
Uncaught ReferenceError: $ is not defined
Upvotes: 1
Views: 1657
Reputation: 415
Try This If you found this error "Uncaught ReferenceError: $ is not defined" than replace "$" to "jQuery".
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 1
Reputation: 37
I put the jQuery file on the head instead of the bottom of the body and it works. Thank you.
Upvotes: 0
Reputation: 3584
Try this:
$('#openModal').on('show.bs.modal', function(e) {
var data_href = $(e.relatedTarget).attr('data-href');
console.log('data_href: ' + data_href);
$(this).find('.btn-ok').attr('href', data_href);
$('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
});
What you did was good, but there was a little mistake in your code:
$(e.relatedTarget).data('href')
You were trying to get the attribute href but the attribute on the button you are looking for is data-href
Upvotes: 0
Reputation: 35
Try using shown
instead of show
$('#openModal').on('shown.bs.modal', function(e) {
Upvotes: 1