Submit event of a submit type input isn't triggered
Clash Royale CLAN TAG#URR8PPP
I wrote a silly example to see which of the two, the click
or the submit
event, fires first on an input of type submit
.
When I run it, though, nothing is written to the console.
Stepping through the code in the debugger reveals the following series of occurrences.
It fires the
click
event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.Also, the
submit
event is never fired.
I infer that the form is getting submitted after executing the click
event of the button. The thing I don't understand is why it isn't getting into my submit
event handler?
Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
javascript
add a comment |
I wrote a silly example to see which of the two, the click
or the submit
event, fires first on an input of type submit
.
When I run it, though, nothing is written to the console.
Stepping through the code in the debugger reveals the following series of occurrences.
It fires the
click
event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.Also, the
submit
event is never fired.
I infer that the form is getting submitted after executing the click
event of the button. The thing I don't understand is why it isn't getting into my submit
event handler?
Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
javascript
add a comment |
I wrote a silly example to see which of the two, the click
or the submit
event, fires first on an input of type submit
.
When I run it, though, nothing is written to the console.
Stepping through the code in the debugger reveals the following series of occurrences.
It fires the
click
event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.Also, the
submit
event is never fired.
I infer that the form is getting submitted after executing the click
event of the button. The thing I don't understand is why it isn't getting into my submit
event handler?
Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
javascript
I wrote a silly example to see which of the two, the click
or the submit
event, fires first on an input of type submit
.
When I run it, though, nothing is written to the console.
Stepping through the code in the debugger reveals the following series of occurrences.
It fires the
click
event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.Also, the
submit
event is never fired.
I infer that the form is getting submitted after executing the click
event of the button. The thing I don't understand is why it isn't getting into my submit
event handler?
Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.addEventListener("submit", function(event)
console.log("submit");
// event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div margin: 20px;
input[type="submit"] width: 200px; height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
javascript
javascript
edited Dec 29 '18 at 9:46
JJJ
29k147591
29k147591
asked Dec 29 '18 at 7:46
Water Cooler v2Water Cooler v2
12.2k29101198
12.2k29101198
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The thing I don't understand is why it isn't getting into my submit event handler?
input
elements don't have a submit
event. You need to hook submit
on the form
, not the input
.
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
(btn.form
there could be btn.closest("form")
if you prefer, though btn.form
is more widely-supported and is (also) standardized. Or, of course, use getElementById
or querySelector
to retrieve the form
element.)
Live Example:
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967705%2fsubmit-event-of-a-submit-type-input-isnt-triggered%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The thing I don't understand is why it isn't getting into my submit event handler?
input
elements don't have a submit
event. You need to hook submit
on the form
, not the input
.
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
(btn.form
there could be btn.closest("form")
if you prefer, though btn.form
is more widely-supported and is (also) standardized. Or, of course, use getElementById
or querySelector
to retrieve the form
element.)
Live Example:
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
add a comment |
The thing I don't understand is why it isn't getting into my submit event handler?
input
elements don't have a submit
event. You need to hook submit
on the form
, not the input
.
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
(btn.form
there could be btn.closest("form")
if you prefer, though btn.form
is more widely-supported and is (also) standardized. Or, of course, use getElementById
or querySelector
to retrieve the form
element.)
Live Example:
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
add a comment |
The thing I don't understand is why it isn't getting into my submit event handler?
input
elements don't have a submit
event. You need to hook submit
on the form
, not the input
.
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
(btn.form
there could be btn.closest("form")
if you prefer, though btn.form
is more widely-supported and is (also) standardized. Or, of course, use getElementById
or querySelector
to retrieve the form
element.)
Live Example:
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
The thing I don't understand is why it isn't getting into my submit event handler?
input
elements don't have a submit
event. You need to hook submit
on the form
, not the input
.
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
(btn.form
there could be btn.closest("form")
if you prefer, though btn.form
is more widely-supported and is (also) standardized. Or, of course, use getElementById
or querySelector
to retrieve the form
element.)
Live Example:
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
(function()
let btn = document.getElementById("btnSave");
btn.addEventListener("click", function(event)
console.log("click");
, false);
btn.form.addEventListener("submit", function(event)
// ^^^^^
console.log("submit");
event.preventDefault();
, false);
console.log("All done now. Click da button, chum, click it I say!");
)();
div
margin: 20px;
input[type="submit"]
width: 200px;
height: 50px;
<div>Look at the console and then click the button, chum!</div>
<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>
edited Dec 29 '18 at 8:13
answered Dec 29 '18 at 7:51
T.J. CrowderT.J. Crowder
679k12112011296
679k12112011296
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967705%2fsubmit-event-of-a-submit-type-input-isnt-triggered%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown