chickentulip
01-16-2012, 05:09 PM
I wrote a script: when the same button is clicked for the first time, it should give a message "Hello World", for the second time "hello Earth". The script is not working.
I don't want anybody to re-write the script, but to look at how i define variables and tell me where I am wrong. I think, this is where the main error is.
my current script just outputs the message "hello world".
my assumption is that functions can alter the value of a global variable.
Thank you very much.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> title</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">
var count=0;
function changePar() {
if(count===0)
first();
if(count===1)
second();
}
function first() {
document.getElementById("changingParagraph").innerHTML="Hello World";
var count=1;
}
function second() {
document.getElementById("changingParagraph").innerHTML="Hello Earth";
var count=0;
}
</script>
</head>
<body>
<button onclick="changePar()">Click Here</button>
<p id="changingParagraph"></p>
</body>
</html>
I don't want anybody to re-write the script, but to look at how i define variables and tell me where I am wrong. I think, this is where the main error is.
my current script just outputs the message "hello world".
my assumption is that functions can alter the value of a global variable.
Thank you very much.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> title</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">
var count=0;
function changePar() {
if(count===0)
first();
if(count===1)
second();
}
function first() {
document.getElementById("changingParagraph").innerHTML="Hello World";
var count=1;
}
function second() {
document.getElementById("changingParagraph").innerHTML="Hello Earth";
var count=0;
}
</script>
</head>
<body>
<button onclick="changePar()">Click Here</button>
<p id="changingParagraph"></p>
</body>
</html>