default jquery append only support .append( content [, content ] )
, how to implement following code:
$(body).append("<p>prpr</p>", function() {
// do smth after append
})
default jquery append only support .append( content [, content ] )
, how to implement following code:
$(body).append("<p>prpr</p>", function() {
// do smth after append
})
don't think you you need a callback, .append()
DOM manipulations is synchronous. so just put the function below the .append()
line
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>
$("div").each(
function(){
$(this).append("<p>prpr</p>");
function() {
// do smth after append
}
}
}