Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   General Forum (http://www.chiefdelphi.com/forums/forumdisplay.php?f=16)
-   -   Help with website (http://www.chiefdelphi.com/forums/showthread.php?t=102595)

Stonemotmot 11-02-2012 20:17

Help with website
 
Hello, my team is working on the website and were trying to do some java script but could not get it to work could someone tell me what is wrong with this simple script.

<html>
<head>

</head>
<body>

<h1>Team #</h1>
<p id="demo"></p>

<button type="button" onclick="displayDate()">Display Date</button>
<script type="text/javascript">

var a =1;
function displayDate()
{
a=a+2;
}

if (a==1)
{
document.getElementById("demo").innerHTML="Hello";
}
else
{
document.getElementById("demo").innerHTML="GoodBye ";
}

</script>

</body>
</html>

HansJ 11-02-2012 22:01

Re: Help with website
 
Add a set of curly braces around the code you want in the function e.g.

Code:

var a =1;
function displayDate()
{
        a=a+2;
        if (a==1)
        {
                document.getElementById("demo").innerHTML="Hello";
        }
        else
        {
        document.getElementById("demo").innerHTML="GoodBye ";
        }
}

The if statement that changes the innerHTML was not being called when you clicked the button, only the addition part

Make sense?

ianonavy 11-02-2012 22:41

Re: Help with website
 
First of all, you should ALWAYS begin your HTML files with a doctype.
Code:

<!DOCTYPE html
Your JavaScript should really be in a separate file that is linked in with something like this before the </body> tag:
Code:

<script type="text/javascript" src="js/index.js"></script>
with all your JavaScript being put in the file "index.js" in a "js" subdirectory.

Second of all, I made a little demonstration snippet using JSFiddle, which can be found here: http://jsfiddle.net/ianonavy/cfHpd/

I hope this helps. Continue to post if you have further questions.

Stonemotmot 12-02-2012 15:16

Re: Help with website
 
Thank you I understand what I did wrong. You were big helps.


All times are GMT -5. The time now is 17:25.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi