2

default jquery append only support .append( content [, content ] ), how to implement following code:

$(body).append("<p>prpr</p>", function() {
// do smth after append
})
chikadance
  • 3,591
  • 4
  • 41
  • 73

3 Answers3

3

don't think you you need a callback, .append() DOM manipulations is synchronous. so just put the function below the .append() line

oreofeolurin
  • 636
  • 3
  • 16
1

In jQuery you can check $(element).length to make absolutely sure that the new DOM nodes have been appended.

Working Example:

$(document).ready(function(){
  
    $('body').append('<p class="newParagraph1">This is a new paragraph, added via a script.</p>');
  
    $('body').append('<p class="newParagraph2">This is a second new paragraph, added via a script.</p>');

    if ($('.newParagraph1').length) {
console.log('The first dynamically added paragraph just loaded.');
    }
  
    if ($('.newParagraph2').length) {
console.log('The second dynamically added paragraph just loaded.');
    }
  
    if ($('.newParagraph3').length) {
console.log('The third dynamically added paragraph just loaded.');
    }

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>This paragraph is already in the markup</p>
Rounin
  • 27,134
  • 9
  • 83
  • 108
0
$("div").each(
    function(){
        $(this).append("<p>prpr</p>");
        function() {
                // do smth after append
        }
    }
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Alex Parloti
  • 668
  • 1
  • 9
  • 25