2

Probably a daft oversight but I have an HTML form similar to this:

<form onsubmit="updateProfile();">
    <input type="submit" value="Update Account">

    ..
    ...

</form>

And:

function updateProfile() {

    // never gets here

};

I set a breakpoint in the updateProfile() function but when I click the Update Account button it never gets there.

Can anyone give me a likely explanation?

Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
Robert
  • 5,278
  • 43
  • 65
  • 115

5 Answers5

2

A form without an action attribute is not a form, according to standards - and will actually cause a page reload in some browsers.

To avoid this behavior update from

<form onsubmit="updateProfile();">

to

<form onsubmit="updateProfile(); return false;">
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
2
<form id="myForm">
    <input type="submit">
</form>

then in javascript you can do this

$('#myForm').on('submit', function(event){
    event.preventDefault();
    updateProfile();
});

and don't forget to import jquery library :)

1

It seems i cannot reproduce your issue, look here is it similar?

codepen http://codepen.io/icrosil/pen/bdXyPL

1
<form onsubmit="updateProfile(event);">
<input type="submit" value="Update Account">

..
...

</form>

function updateProfile(e) {
e.preventDefault();
 alert("it gets here buddy");

};
Master Yoda
  • 531
  • 8
  • 22
1

May be a click handler is active somewhere in the document, catches the click event, prevents the default action to occur (returning false or using event.preventDefault()), hence the submit event never triggers from the form.

Hibou57
  • 6,870
  • 6
  • 52
  • 56