Solved

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

Posted on 2004-10-31
269 Views
Last Modified: 2012-05-05

 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.
0
Question by:b_rajathilagam
    6 Comments
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    try this

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

    Expert Comment

    by: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>
    0
     
    LVL 31

    Accepted Solution

    by:
    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
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    function t()   not    function ()       in 2nd line (still works though)
    0
     
    LVL 12

    Expert Comment

    by: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>
    0
     
    LVL 7

    Expert Comment

    by:Xxavier
    why the B?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: JavaScript Coding - Massive 12-Part Bundle

    Regardless of your programming skill level, you'll go from basics to advanced concepts in a vast array of JavaScript subjects including Sammy.js, Agility.js, Ember.js, Node.js, jQuery, AJAX, Extjs, AngularJS, Knockout.js, and JSON.

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now