Server_Mule
Server_Mule

Reputation: 589

Function call in JavaScript

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>

Upvotes: 0

Views: 537

Answers (5)

vireshas
vireshas

Reputation: 816

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>

Upvotes: 0

Buddy
Buddy

Reputation: 6713

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.

Upvotes: 2

Spike Williams
Spike Williams

Reputation: 37295

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>

Upvotes: 2

eKek0
eKek0

Reputation: 23289

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

Upvotes: 0

Praveen Angyan
Praveen Angyan

Reputation: 7265

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

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

Upvotes: 0

Related Questions