0

We have a javascript function which gets called on click of an image in an HTML page. Inside the function, we explicitly submit the form using

$('#myForm').get(0).submit();

Is there any way for me to capture event of this submit() getting called and then again do a submit after modifying something in the form?

Also, while doing this, I do not know the exact name or id of the form.

  • 2
    $('#myForm').on("submit", function(e) { e.preventDefault(); //do your actions here// }); – Pieter De Clercq May 23 '17 at 21:02
  • You can use `$.ajax()`, `FormData` to submit `multipart/form-data` to server – guest271314 May 23 '17 at 21:02
  • @thepieterdc, I should edit my question to include this - but can I do this when the form name and id are not known to me? – user8055937 May 23 '17 at 21:05
  • @user8055937 you could try something like ```$("form").on("submit", function(e) { e.preventDefault(); //code here// });``` I am not sure whether this will work but it should capture all submit events on all forms on the HTML page. – Pieter De Clercq May 23 '17 at 21:06
  • here you can define your submit behavior $.fn.submit = function() { // whatever you want to do } and you call it $(el).submit(). Last example https://api.jquery.com/submit/ – daremachine May 23 '17 at 23:10
  • The link posted by @Ursache worked well for my purpose - https://stackoverflow.com/questions/5384712/capture-a-form-submit-in-javascript%3E - I used the idea of overriding submit method (last post by Vitaliy). What I dont get though is what is the purpose behind the 2 code snippets in that post. Thanks all for the answers. – user8055937 May 24 '17 at 13:21
  • Pretty much all the answers here seem to work. The one from @Ursache works best for me - seems to cover all cases. Would like to mark it as correct - seems I need to have more points to do so. – user8055937 May 24 '17 at 13:47

0 Answers0