-1

I have the following HTML

<form onsubmit="call_it()" action="listing.php" method="get">

In the called function the alert('3') is not getting called. The jQuery each function is working perfectly but not sure why nothing is getting executed after that.

function call_it() {
  alert('2');

  $('.form_field').each(function(i, obj) {
    if (this.value == "") {
      document.getElementById(this.id + '_blank').style.display = "block";
    }
  });

  alert('3');
  ///other statements
}
Christoph
  • 50,121
  • 21
  • 99
  • 128
Monty
  • 13
  • 5

1 Answers1

1

I am calling this on <form onsubmit="call_it()" action="listing.php" method="get">

In this case the issue is because the form is actually being submit. Hence the page is unloaded, and all JS processing is cancelled, before alert('3') is reached.

To fix this, first remove the outdated on* event handler and use unobtrusive JS to attach your event handlers, and secondly, stop the form submission if the validation fails. Try this:

<form action="listing.php" method="get" id="theForm">
  <!-- HTML inputs here... -->
</form>
$('#theForm').submit(function(e) {
  alert('2');

  $('.form_field').each(function(i, obj) {
    if (!this.value) {
      e.preventDefault();
      $('#' + this.id + '_blank').show();
    }
  });

  alert('3');
});

Working example

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • BRB, building a fiddle example as someone obviously doesn't believe me – Rory McCrossan May 17 '17 at 15:37
  • yeah no idea for downvote (we'll hunt you down and make you upvote xD) – treyBake May 17 '17 at 15:37
  • @ThisGuyHasTwoThumbs that makes absolutely no difference to compatibility. `submit()` calls `on('submit', fn)` internally – Rory McCrossan May 17 '17 at 15:38
  • 1
    I didn't DV, but you gotta know this is a duplicate question... – Heretic Monkey May 17 '17 at 15:38
  • 1
    @ThisGuyHasTwoThumbs there's no difference between the two other than another function nesting. there's nothing "better" about it. – Kevin B May 17 '17 at 15:40
  • @MikeMcCaughan you're probably right, although I couldn't find a suitable one. If you can link one I'll Mjolnir this question – Rory McCrossan May 17 '17 at 15:41
  • Excellant @RoryMcCrossan Its working fine. Thanks a ton mate. – Monty May 17 '17 at 15:42
  • Already VtC :). – Heretic Monkey May 17 '17 at 15:42
  • Yep, just noticed it tucked away :) @Monty no problem, glad to help – Rory McCrossan May 17 '17 at 15:43
  • still waiting to see fiddle.... I would have expected this function to run to completion (thus alerting both times) since you can return false to an onsubmit= to cancel a submit. – Kevin B May 17 '17 at 15:43
  • @KevinB sure you can do that too, but OP wasn't. Here's the working fiddle: https://jsfiddle.net/c4k1r4cg/, and this is OPs original displaying the described behaviour where `alert('3')` is skipped: https://jsfiddle.net/4wjr00sh/ – Rory McCrossan May 17 '17 at 15:43
  • 1
    @ThisGuyHasTwoThumbs there isn't one. If you want event delegation, you use .on. Doesn't make it better than .submit for non-event delegation. – Kevin B May 17 '17 at 15:46
  • @RoryMcCrossan right... but... even if you're not returning, the function has to complete to see what the return value is... the submission hasn't started yet at that point, whether you're returning or not. I'd suspect the user is actually getting an error inside the .each, or a jquery is not defined error. – Kevin B May 17 '17 at 15:48
  • Yeah, that's pretty much it. `on()` can be delegated, whereas the shortcut event handlers (`click()`, `mouseup()`, `submit()` etc) cannot – Rory McCrossan May 17 '17 at 15:48
  • Great thanks :) will delete my comments :D – treyBake May 17 '17 at 15:49
  • @RoryMcCrossan you mean... the fact that it's submitting due to jquery not being defined? here i clearly get both alerts: https://jsfiddle.net/4wjr00sh/2/ – Kevin B May 17 '17 at 16:14
  • @KevinB I am a moron. The amount of times I forget to include a library in a fiddle isn't even funny. Well I'm not sure about the OPs case then, but he claims this did solve the problem. Perhaps there was another event handler which immediately caused the form submission then. – Rory McCrossan May 17 '17 at 16:15