Search code examples
javascriptjquerypopupbpopup

Attach a jquery event after object is created


I am using the bPopup jquery library.. the syntax to add to the onclose event is pretty straightforward:

$('element_to_pop_up').bPopup({
     onOpen: function() { alert('onOpen fired'); }, 
     onClose: function() { alert('onClose fired'); }
})

What I want to do is add something to the onClose event after the object is created.. is it possible?


Solution

  • You can access the bPopup object which will be present inside the data of the element.

    $('element_to_pop_up').bPopup({
        onOpen: function() { alert('onOpen fired'); }, 
        onClose: function() { alert('onClose fired'); }
    });
    
    $('element_to_pop_up').data('bPopup');
    

    NOTE: There is no guarantee that the created object will be always present in element's data. But this is widely used approach. It is better to rely on the callback provided.