+ 8

In a function javascript how can we see the document changing effects first and then the alert () window?

in this example I want to see first how the text in paragraph "something" is changing in "new paragraph" and then the alert window. EXAMPLE: <body> <button onclick="myFunction()">change the paragraph </button> <p id="paragraph"> something </p> <script> function myFunction(){ var text=document.getElementById ('paragraph'); text.innerHTML="new paragraph"; var newText=document.getElementById ('paragraph').innerHTML; alert (newText); } </script> </body>

10th Mar 2017, 4:59 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
7 Réponses
+ 7
This is the only solution I've got for now <script> var newText; function myFunction(){ var text=document.getElementById ('paragraph'); text.innerHTML="new paragraph"; newText=document.getElementById ('paragraph').innerHTML; setTimeout(showAlertWindow,1000); } function showAlertWindow(){ alert (newText); } </script> If you have anything else please post your code
10th Mar 2017, 6:32 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 5
@Patrik can you modify my Example? because I don't understand window.onload is when the page is loaded
10th Mar 2017, 5:21 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 5
should I use a timer?
10th Mar 2017, 5:31 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 5
@Patrik în my Example if you run it ......first you see the alert message and then the new paragraph on the page Well I would like to see first the NEW PARAGRAPH in the page and after 1 second the alert window
10th Mar 2017, 5:51 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 5
Sorry Didi, this one setTimeout( function(){alert(newText)}, 1000);
10th Mar 2017, 6:05 AM
Patrik Sokol
Patrik Sokol - avatar
+ 4
After one second? Oh then yes, you need this setTimeout(alert(newText), 1000);
10th Mar 2017, 5:58 AM
Patrik Sokol
Patrik Sokol - avatar
+ 1
<!DOCTYPE html> <html> <head> <title>Page Title</title> <script> var x = ""; var y; var z = ""; function pChange() { x = document.getElementById("ch").innerHTML; document.getElementById("ch").innerHTML = "Extra Cheese"; y = setInterval(chAlert, 1000); } function chAlert() { z = document.getElementById("ch").innerHTML; var resp = "You changed " + x + " to " + z + "!"; alert(resp); clearInterval(y); } </script> </head> <body> <p id="ch">Cheese</p> <button onclick="pChange()">Change</button> </body> </html>
11th Mar 2017, 5:13 AM
sniperisdemoman
sniperisdemoman - avatar