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.
b_rajathilagamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
try this

<form action="http://google.com/search" onsubmit="alert('hello world');return false">
 <input type='text' name=q>
 <input type='submit'>
</form>
0
devicCommented:
<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>
0
GwynforWebCommented:
or this


<input type='button' value='Enable Submit' onclick="document.f1.onsubmit=function s(){return true}"><br>
<input type='button' value='Disable Submit' onclick="document.f1.onsubmit=function (){alert('hello world');return false}"><p>

<form name=f1 action="http://google.com/search" onsubmit="alert('hello world');return false">
 <input type='text' name=q>
 <input type='submit'>
</form>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:
function t()   not    function ()       in 2nd line (still works though)
0
minichickenCommented:
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>
0
XxavierCommented:
why the B?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.