Delay the alert
by using a setTimeout
so that the page has time to re-draw.
When you use alert
all script execution is halted on the page (eg: the page is basically frozen). But when you call $.text()
the actual painted data of the text won't be updated until the next paint
call done by the browser.
Actually, by using text
you also trigger the layout
phase of re-rendering the page. So, what happens in this case is you update the text value of that DOM node and the browsers will try to update the graphically displayed data when it is "free", but because you trigger an alert immediately the browser won't be free until you close that alert. By using the setTimeout
you make sure that the browser is "free" before showing the alert so the text data can be updated.
Read this to understand more about how browsers render the page: https://developers.google.com/web/fundamentals/performance/rendering/
DEMO: https://jsfiddle.net/n2n5drL2/3/
$('#change').click(function(){
$('#test').text('new content');
setTimeout(function() {
alert('how to make this alert shows after the div changes its content?');
}, 0);
})