www.webdeveloper.com
Results 1 to 5 of 5

Thread: Simple Question on HTML Form Submission

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Question Simple Question on HTML Form Submission

    Hi,

    Just found out that invoking the onclick() of a SUBMIT button doesnt submit the form even though the function gets called and returns true. Here's a snippet. Can anyone tell me why? In this example, clicking the first INPUT submits,but doesnt submit on second INOUT click.
    (Guys, I know onclick() works only on IE,but that's not my point here )
    Code:
    <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Why?</title>
    </head>
    <script>
    function test()
    {
    	return true;
    }
    function clickHandler()
    {
    	document.getElementById("mybutt").onclick();
    }
    </script>
    <body>
    <form action="www.google.com">
    	<input type="submit" onclick="test();" id="mybutt"> 
    	<input type="button" onclick="clickHandler();" >
    </form>
    
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    onclick is an event listener
    click is an event

    AND, if you are trying to do something at the time of form submission, you don't want to use an onclick listener on the submit button, you want an onsubmit listener on the form. Try having return false in your test() function and see if it makes any difference....it doesn't.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    I see.. so onclick() doesnt really simulate a button click event which would submit the form,but rather just calls the event handler function... Thanks!

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    Sorry, I should've explained myself better.

    onclick is a property of DOM elements which usually stores a method.
    So when you call
    Code:
    document.getElementById("mybutt").onclick();
    You are effectively doing nothing.

    What you were attempting to do is accomplished by this code:

    Code:
    document.getElementById("mybutt").click();
    http://www.roseindia.net/javascript/...k-method.shtml
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Aug 2010
    Posts
    3
    hmm..that's interesting..so onclick() just executes the event handler function,but click() executes the event..I just checked, click() works on all browsers.. thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles