0

Let's keep this short and sweet.

Here is my header:

<head>
 <title>4JSB Assignment</title>
 <link rel="stylesheet" type="text/css" href="css/style.css">
 <script type="text/javascript" src="javascript/form.js"></script>
</head>

Note: <script type="text/javascript" src="javascript/form.js"></script>

Does not appear to be working.

I have a Submit button in the body that is part of a form. Here it is, located at the end of the aforementioned form:

<input type=submit name="submitForm" id="submitForm" onclick="submitForm()">

Here is my external javascript:

function submitForm() {
alert("Working");
}

Alas, "Working" never appears.

My folder structure is as follows:

root
 css
 ....style.css
 javascript
 ....form.js
 form.html

The answer is more than likely trivial, but has had me stuck on this assignment for hours because of this one requirement that the javascript be linked from an outside source. I appreciate any attempt to point out this mundane and unfortunate mishap to me.

2
  • check the network tab of your browsers developer tools - is form.js loading? also, if you're using IE/Edge it's particularly annoying when using javascript if the page is loaded using file:/// protocol Commented Dec 6, 2015 at 10:59
  • 1
    go through the page click right button and "click view page source".Then click <script type="text/javascript" src="javascript/form.js"></script> and check is this ok or not? Commented Dec 6, 2015 at 11:02

3 Answers 3

3

The issue is that you have id="submitForm" and function submitForm

Not sure why browsers do this, but any id is available as a global object

so,

console.log(submitForm);

would show the input element, rather than the function!!

use a different name for the id, or for the function

console.log(submitForm) actually shows the function!! but it's still a name conflict in the end.

Praveen Kumar Purushothaman
167k27 gold badges215 silver badges262 bronze badges
answered Dec 6, 2015 at 11:08
Sign up to request clarification or add additional context in comments.

4 Comments

This was the issue. Thanks very much.
Whit that change, does your form actually submit?
there was nothing in the question about submit/no submit, so it's a cows opinion
I didnt downvote your question so stop the witch hunting. I guessing @JosiasMenzel want to submit the form otherwise why the use of a submit button??
1

Try changing the name and id of your submit button to something like "submitButton" so that it isn't exactly the same as your javascript function. I believe there is a name conflict.

answered Dec 6, 2015 at 11:07

1 Comment

Did exactly this when Jaromanda commented about the id issue. It is working now. Thank you.
0

It depends on what do you want to acomplish:

If you add a onclick function on your submit button it wont work for submit the form, so it will be pointless to have it as that.

If you want execute a javascript function before submit the form and or want to perform some validations that may or may not prevent the form for being submitted . The best way to do it:

<form onsubmit="return submitform();">
 ....
 <input type=submit name="submitFormAny" id="submitFormAny">
</form>

Also as other contributors were saying, be careful, you can't have elements and functions with same id's

answered Dec 6, 2015 at 11:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.