0

I'm stumped, I can't seem to get this simple Javascript function to get called. Thanks!

 <html>
 <head>
<script type="text/javascript">
 function increase()
 {
 alert(" the button was pressed");
 }
</script> 
 </head>
 <body>
 <form action="Test.html" method="post">
 <input type="submit" onclick="increase();" />
</form> 
 </body>
 </html>
Dan Lew
87.8k33 gold badges184 silver badges177 bronze badges
asked May 8, 2009 at 23:47
1
  • Well, you're not calling it anywhere we can see. That would explain the problem. Commented May 8, 2009 at 23:59

5 Answers 5

2

It is hard to tell where you are going wrong. It looks like you are just defining a function. This will case the increase function to run when the page is loaded:

<script type="text/javascript">
 function increase() {
 alert(" the button was pressed");
 }
 increase();
</script>

This will run the function when a button is pressed.

<script type="text/javascript">
 function increase() {
 alert(" the button was pressed");
 }
</script>
<button onclick="increase()">text</button>

It sounds like you are just getting started, and that is awesome. I would also suggest getting a book. A few years ago, I read DOM Scripting by Jeremy Keith, it was okay. Also, try looking around online for tutorials.

answered May 9, 2009 at 0:09
Sign up to request clarification or add additional context in comments.

2 Comments

does the button have to be in a Form tag? if so, how should it look, Thanks
Nope - doesn't have to be a part of a form.
2

Try this:

<input type="button" id="buttonId">Button</input>
<script language="javascript">
 function increase() { alert(" the button was pressed"); } 
 document.getElementById("buttonId").onClick=increase;
</script>
answered May 8, 2009 at 23:59

Comments

0

Have you tried this? You need to place JavaScript between script tags:

<script type="text/javascript">
 function increase() { alert(" the button was pressed"); } 
</script>
answered May 8, 2009 at 23:49

Comments

0

Apart from what Praveen said, wich I agree, there are good beginner tutorials here and here.

answered May 8, 2009 at 23:54

1 Comment

0

in your code the form submits as you click the button.
what you will have to do is return false; so that the form doesnt submit..

<form action="#" method="POST">
 <input type="submit" onclick="return false; increase();">
</form>
answered Feb 1, 2012 at 12:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.