+ 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>
7 Answers
+ 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
+ 5
@Patrik can you modify my Example?
because I don't understand window.onload is when the page is loaded
+ 5
should I use a timer?
+ 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
+ 5
Sorry Didi, this one
setTimeout( function(){alert(newText)}, 1000);
+ 4
After one second? Oh then yes, you need this
setTimeout(alert(newText), 1000);
+ 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>