Click to See Complete Forum and Search --> : Buttons and onClick command


Eburris
10-06-2003, 02:00 PM
hello. I am currently trying to learn HTML and Java script. I am 14 years old and this is what i want to do for my science fair project. anyway here is the problem.

I was working out of the book "HTML and Web Design" by Kris Jamsa,m Konrad King, and Andy Anderson on biginning using buttons and functions. they gave this code to insert into the web editor and i did so:




<html>
<head>
<body>
<form>
<input type="button" value="Click Me!"
onClick="youClickedIt ()">
</form>
</body>
</head>
</html>


this was supposed to show up as a button that says click me and when you click it it was supposed to say you clicked it. But the first time i tried it i clicked the button and it gave me a runtime error stating an object was undefined. the second time as soon as the browser came up it gave me a runtime error on line five stating "Object expected" what is my scripts' problem?
and i know this is a newbie question but i am interested in learning this.

thank you for your time

gil davis
10-06-2003, 02:48 PM
What you have posted is not a script. It is just HTML. You need a script with a function called "youClickedIt()" for it to work. As it is, when you click the button, you will get a runtime error because the browser cannot find the function. The whole file should look something like this:
<html>
<head>
<title>This is my first script page</title>
<script language="JavaScript" type="text/javascript">
function youClickedIt() {
alert("Hey! Stop clicking my button!");
}
</script>
</head>
<body>
<form>
<input type="button" value="Click Me!" onClick="youClickedIt ()">
</form>
</body>
</html>
Note also that the BODY tag needs to be outside the HEAD tag.

simpson97
10-06-2003, 02:49 PM
Try this.
You had the </head> tag in wrong place & didn't have a function.
<html>
<head>
<script type="text/javascript">
function youClickedIt() {
alert ("You clicked me!")
}
</script>
</head>
<body>
<form>
<input type="button" value="Click Me!" onClick="youClickedIt ()">
</form>
</body>
</html>

Bob

Eburris
10-07-2003, 07:31 PM
ooohh. Duh. excuse my newbieness. I'm still learning. :D
thanks guys its working now. but now I have another question. For my 8th grade science project I have two scripts for different styles of clocks and I wanted to demonstrate how they work, some of the Functions used and I wanted to create a button that when you click it it changes the form of the clock to the other style. is there a script for changing what you see on the screen to something different with out using window.open?