Skippy Skippy - 6 months ago 40
Javascript Question

get bootstrap modal element id on hide.bs.modal

I am looking to return the element ID of the currently being hidden modal in bootstrap. I cannot seem to find a way to capture this via this code:

$(document).on('hide.bs.modal', function (e) {

// I want to know what the attr ID is of THIS modal being closed?
// I tried console.log(e) to see if I could find the reference but its not in the object?

});


The purpose is to have this for all modals, some modals contain input fields, if changed to a dirty flag I can alert the user that data loss may occur if they close this modal.

Answer

If you log the event you receive inside that handler function, you'll notice there is a target node on their that contains the actual modal. So getting the id should be as simple as:

var id = e.target.id;

have a look at the demo I set up:
http://www.bootply.com/JvORc5bWvD
On close you'll see the modal id in the console.

Comments