1

Possible Duplicate:
submit is not a function in javascript

I am trying to submit form using JS, but no luck. Here is the code. JSFIDDLE

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    function validate () {
        alert('start');        
        $('#myform').submit();
        alert('end');        
    }
    </script>
</head>
<body>
    <form id="myform" name="myform" method="post" action="">
        <input type="button" value="submit" id="submit" onclick="validate();" >
    </form>
</body>
</html>

The question is "Why its not working?".

2
  • What does not working mean? Is there an error on the page? Commented Oct 13, 2012 at 19:37
  • Is the alert getting called ? Commented Oct 13, 2012 at 19:43

5 Answers 5

10

You have an form element with the id submit which conflicts with the form method submit, change the submit button id to something else.

You can access form elements as properties of the form object, so if you have a form say myForm and an input in the form with id or name submit then myForm.submit will be the input element with id submit and not the original submit method.

See http://jsfiddle.net/4kg8c/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Is that mean, we shouldn't name an ID to any JS function, seems strange. one is ID and other is function.
@Dev you can access form elements as properties of the form object, so if you have a form say myForm and an input in the form with id or name submit then myForm.submit will be the input element with id submit and not the original submit method.
3

The error in the console is

Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function 

The problem is you hijacked the sumit() function by naming an element submit. Change the button's id to btnSubmit.

2 Comments

great!!! that's the point but the ID is submit, how can that hijack submit function of JS ?
Do a console.log(document.forms[0].submit); You will see that the element is referenced and not submit. The id will take place of the name. Hence why you get the error. Never name element's submit with an id or name.
1

I have modify your code with solution use that.

submit button's name is missing. and don't use name as "submit" of submit. it conflicts with JS so.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
    function validate () {
        document.myform.submit();   
    }
    </script>
</head>
<body>
    <form id="myform" name="myform" method="post" action="">
        <input type="button" name="save" value="submit" id="submit" onclick="validate();" >
    </form>
</body>
</html>

1 Comment

that's not working. There is nothing wrong with jQuery code.
0

If this is the exact code you have written, then I think you have ot included jQuery library in the page.

You need to include jQuery file before you can use the $("#myform") notation.

Also specify some action URL where you want to submit the page to ?

Comments

-1

You use jQuery code (the $ function) but you do not include the jQuery source.

1 Comment

I had posted link to jsfiddle, please have a look.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.