Link to home
Start Free TrialLog in
Avatar of b_rajathilagam
b_rajathilagam

asked on

How to stop submission of form while pressing the submit button?


 Hi,
     
               I wants to stop the submission of form while pressing a submit button. I will call a function to do some operation while pressing the submit button. i don't want to submit the form instead i wants to call a function.
                         
                     how can i do this through javascript?

Thanks.
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

try this

<form action="http://google.com/search" onsubmit="alert('hello world');return false">
 <input type='text' name=q>
 <input type='submit'>
</form>
Avatar of devic
<script>
function someCalculation(obj)
{
      notCalculated=true;
      if(notCalculated)
      {
            alert("calculation");
            return false;
      }
      return true;
}
</script>

<form action=http://www.google.com/search/? onsubmit="return someCalculation(this)">
      <input type=text name=q value=hurra>
      <input type=submit>
</form>
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
function t()   not    function ()       in 2nd line (still works though)
Avatar of minichicken
minichicken

Hi

If you just want a button to call a function and not to submit the form, then you can just use input type ="button" instead of input type ="submit". If you however would like to submit through the function and can use form_name.submit();

EXAMPLE:
***************************************************

<script language ="javascript">
function some_func()
{
     alert ("Some function");
     //form1.submit(); //this submits the form
}
</script>

<form name="form1" action="" method ="post">
  <input type = "button" name = "btn1" value = "click button">
</form>
why the B?