Pass data attribute to modal bootstrap
You can listen for show.bs.modal
event on modal and get the clicked element available as relatedTarget
property of the event. Check Bootstrap modal documentation for further reference.
Here is a working example using Bootstrap v4.
$('#my-modal').on('show.bs.modal', function (event) { var myVal = $(event.relatedTarget).data('val'); $(this).find(".modal-body").text(myVal);});
<!-- Bootstrap CSS --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<a href="#" class="my_link" data-val="user1" data-toggle="modal" data-target="#my-modal">Open Modal</a>
<div class="modal fade" id="my-modal" tabindex="-1" role="dialog" aria-labelledby="my-modal" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">My Modal</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div></div>
<!-- jQuery, Popper and Bootstrap JS --><script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
Passing data-id from button to bootstrap modal
I usually use .attr
for data-* attributes and it works.
$(this).attr('data-id');
Passing Data-id of an Object to Bootstrap Modal form
You were nearly there though you were not targeting
the right element
in your modal open.
Since you have an a
element and button
is inside the a
which contains the data-id
You need to watch for the eventTarget
and then use .find() method to find the button and get the data id from it which will be passed via ajax
var id = button.find('button').data('id') //need to find the button and get id
In addition, you also need an event handler which will click
function inside your modal open
which will trigger when you click on Delete task button in your modal.
Lastly I have added a modal close
option as well which will happen ajax success - I have fixed up your code
and is working as intended.
Live Working Demo: (Showing the data-id in console.log and click button working)
$("#deleteTaskModal").on('show.bs.modal', function(event) {
var button = $(event.relatedTarget) //Button that triggered the modal
var id = button.find('button').data('id') //need to find the button and get id
var url = '/delete/' + id; //url
console.log('data-id= ' + id) //15
//Click delete task in modal
$(document).on('click', '.delete_task', function() {
if (confirm('Delete task')) {
$.ajax({
url: url,
type: "DELETE",
success: function(result) {
$('#deleteTaskModal').modal('hide') //hide modal on success
console.log("Deleting task...");
window.location.href = '/';
},
error: function(err) {
console.log(err);
}
})
}
})
})
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<span data-toggle="tooltip" data-placement="bottom" title="Delete task">
<a data-toggle="modal" data-target="#deleteTaskModal">
<button class="deleteTask fas fa-trash-alt" data-id='15'></button>
</a>
</span>
<div class="modal fade" id="deleteTaskModal" tabindex="-1" aria-labelledby="exampleformModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="" action="" method="">
<div class="modal-header">
</div>
<div class="modal-body">
<h3>Do you want to delete this task?</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary delete_task">Delete Task</button>
</div>
</form>
</div>
</div>
</div>
</div>
How to pass data to normal CSS/JS modal
You can just add data-*
attributes to your modal's main container and use it later.
<div id="myModal" class="modal" data-foo="bar"> <!-- ... --> </div>
// Get the modal
var modal = document.getElementById("myModal");
/*...*/
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
// append data-foo value to the modal's content
modal.querySelector('.modal-content p').innerText = modal.dataset.foo;
}
Here's an example of how datasets work: https://codepen.io/kmsdevnet/pen/ExggvyP
dataset documentation:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/dataset
Related Topics
How to Detect Browser Refresh Using Angular
How to Apply the Length Condition on the Existing Regular Expression
Instead of Downloading, Files Are Opening in Browser
How to Loop Through Json Array and Get Specific Values
Passing Dynamic JavaScript Values Using Url.Action()
Prevent React-Router History.Push from Reloading Current Route
How to Change the Font Color, Font Size, and Button Color in React
How to Download a Base64-Encoded Image
How to Redirect Drop Down Menu When Option Is Selected
Regex for Names Validation Allow Only Letters and Spaces
How to Reset the Bootstrap Modal When It Gets Closed and Open It Fresh Again
Postman: How to Check Whether the Field Is Returning Null in the Postman Automation
React-Router Urls Don't Work When Refreshing or Writing Manually
React Jsx - Make Substring in Bold