Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Overriding a javascript onsubmit function

Options
  • 03-08-2011 11:05am
    #1
    Registered Users Posts: 377 ✭✭


    I need to override the onsubmit attribute of a javascript function. And the following code does the job -

    [HTML]<html>
    <body>
    <form onsubmit="original_function()">
    <input type="submit">
    </form>

    <script>
    function original_function()
    {
    alert("Original");
    }

    function override_function()
    {
    alert("Overridden");
    }
    var form = document.getElementsByTagName("form")[0];
    form.onsubmit = override_function;
    </script>
    </body>
    </html>[/HTML]However the particular onsubmit attribute I need to override is in the following form - [HTML]<form onsubmit="return original_function(var1, var2)"[/HTML]As you can see it conatins the 'return' keyword and two variables. So how am I supposed to specify that when overriding the function? Because the following doesn't work -

    [HTML]<html>
    <body>
    <form onsubmit="original_function()">
    <input type="submit">
    </form>

    <script>
    function original_function()
    {
    alert("Original");
    }

    function override_function(var1, var2)
    {
    alert("Overridden");
    }
    var a = 1;
    var b = 1;
    var form = document.getElementsByTagName("form")[0];
    form.onsubmit = "return override_function(a, b)"; // This doesn't work
    </script>
    </body>
    </html>[/HTML]


Comments

  • Registered Users Posts: 255 ✭✭boblong


    Something like this?
    <html>
    <body>
        <form onsubmit="original_function()">
            <input type="submit">
        </form>
    
        <script>
            function original_function()
            {
                alert("Original");
            }
    
            function override_function(var1, var2)
            {
                alert("Overridden");
            }
            var a = 1;
            var b = 2;
            var form = document.getElementsByTagName("form")[0];
            form.setAttribute("onsubmit","return override_function(a, b)"); // This does work :)
          </script>
    </body>
    </html>
    


  • Registered Users Posts: 377 ✭✭irishdude11


    Great, that works alright. Cheers mate, had been googling for ages on how to do this.


Advertisement