3

We have a large application with about a 100 jsp pages. When we submit a form (using javascript), the cursor does not change. Is there a way to dynamically intercept the form submit and change the cursor using jquery? Unfortunately, a selector using a named form may not be possible; also, want to keep the function generic for all forms by including it in a common js file.

I tried the following code, but did not work. Is there a way to have a selector similar to what I'm trying below?

function cursorwait(e) {
    document.body.className = 'wait';
}

$(function () {
    var jspform = $('form');
    var fmsubmit = jspform.onsubmit;

    jspform.onsubmit = function () {
        cursorwait();
        if (fmsubmit) {
            fmsubmit.call(jspform);
        }
    }
});
karthikr
  • 97,368
  • 26
  • 197
  • 188
Bob76
  • 477
  • 1
  • 5
  • 12

1 Answers1

4
$('form').submit(function(){
  document.style.cursor = 'wait';
});

very primitive, but I believes performs what you're asking for.

follow-up:

  • .submit() is an event binder provided by jQuery (so no need to try to use onsubmit and be cross-browser compliant)
  • This only "adds functionality" to the submit--it won't interfere, reject or otherwise block a form submit (e.g. using client-side validation and the form failed)--this would require additional logic to handle this circumstance.
  • I applied the cursor style to the whole document, but you can add the body class just as easily within the same code block.
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • I tried the above, but the submit never seems to reach the jquery function. I tried adding an alert to confirm and the alert never fires. Does the sequence of the above function and the form declaration in the page matter? – Bob76 Apr 18 '13 at 15:38
  • wrap the code in `document.ready` event handler as I showed in my answer – Igor Apr 18 '13 at 16:28
  • yes, i had tried wrapping it within $(function(){} and was still seeing the same results – Bob76 Apr 18 '13 at 16:39
  • does jquery javascript successfully load into the page? – Igor Apr 18 '13 at 16:41
  • one additional observation: in pages where the carriage return submits the form, the above code works okay. In pages where the carriage return does not work and a mouse click is required to call a javascript submit, it does not work. Maybe i should add a hidden input-submit to the pages? – Bob76 Apr 24 '13 at 15:38
  • @user439473: Can you provide an example? Works on [all cases](http://jsfiddle.net/DQcMT/) for me... – Brad Christie Apr 24 '13 at 15:53
  • just for reference, this probably doesn't work because user has another submit checking applied to the form and if that submit checking fails, this even code doesn't even exectute. Was my case anyway. – Tyler Dec 12 '18 at 01:28