Why do both buttons call my function?
I have a form:
and Javascript:
My issue is that all the buttons for some reason call and activate the event that should only run on submit
Only the submit button has 'type="submit"', I also tried adding "submitter" as an attribute altho I don't think that's an actual thing
16 Replies
try giving the other buttons a type="button"
in some browsers the default type is "submit" so they will be submitting the form
I'll try that, but if that works, oh, yeah that's what I was about to ask
give me a moment I'll try that right away
you also might want to use type="reset" on your cancel button
When they are inside a form, buttons have a default type of "submit".
That did the trick, I didn't know this
Thanks!
why's that?
by "some" browsers it appears that it is all browsers other than IE (just googled it)
I was planning on just closing the modal (using the javascript to close it) on clicking cancel
so by this logic, do you mean that outside of forms they do not have submit as default?
of course it depends on what you are doing with that button - in your case "reset" probably isn't the right one
they will have the default type submit but if they are not within a form there is nothing to submit
Not by default, but you can associate buttons with a specific form if you have multiple.
Oh, I didn't know
I thought the default would just be, you know, button... Would make sense to me that a button is by default
type="button"
no matter whatYeah, I don't blame you 😄
Well thanks for the quick help and information, I thought I was going crazy
Been looking online too (on for example w3schools) but I couldn't find information about it being submit by default, or I just skipped over it accidently
MDN to the rescue for anything related to the web:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#notes
I should probably use that one more often, it seems everyone here sends a link to this whenever they help out
Honestly, if there's one thing that Mozilla does really well is writing documentation, at least for the web.
Yeah, I clicked on it and I noticed
It is more organized and more complete than what I used