0

How do I call a jQuery function after the data is successfully post and display. Example is as below,

<script>
$(document).ready(function(){

    $("#pr_no").change(function(){
        var pr_no = $('#pr_no').val();  

        $.post('../view-detail-purchase-pdf.php',
        {
            pr_no : pr_no
        }, function(data) {
            $('#result').html('<a href="../view-detail-purchase-pdf.php?pr_no=' + pr_no + '" class="popupwindow">' + pr_no + '</a>');
        })

    }); 

});
</script>

On the $('#result') div is a link that enable a pop up window if click using a jQuery plugin that have the class="popupwindow". How do I make it work.

Amran
  • 627
  • 3
  • 11
  • 30

2 Answers2

3
var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" ); // this is what you may be looking for
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
1

Since the link is dynamically added you will need to delegate the click event for you popup

$('body').on('click','.popupwindow',function(){
// code for showing the popup window
});

or if you have a plugin you define it in the success function

$.post('../view-detail-purchase-pdf.php',
        {
            pr_no : pr_no
        }, function(data) {
            $('#result').html('<a href="../view-detail-purchase-pdf.php?pr_no=' + pr_no + '" class="popupwindow">' + pr_no + '</a>');
           $('.popupwindow').initPopup();//initiate your popup after appending the element on the page;
        })
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • 1
    **Thanks for the help**. Now I understand, we have to put the code after the success function is call. – Amran Jul 14 '16 at 06:23