javascript - Submitting form when losing focus -


i have dynamically created form submit event listener attached it, can ajax requests save data.

but want submit when losing focus.

i make call form.submit() event listener, created, ignored.

form.addeventlistener('submit', function (event_handler) {     event_handler.preventdefault();     /* code */ });  // if loses focus form.addeventlistener('blur', function (event) {    form.submit(); // submits, ignores defined eventlistener preventdefault() ignored.    return false; }, true); 

i expecting form.submit() run code defined inside submit listener.

any way go around this.

no jquery or other frameworks. vanilla js.

there problem indeed. when call javascript form.submit() override @ times event listener. simple solutions have submitter function call , call it.

 function runbeforesubmit()  {      /* code */  }    form.addeventlistener('submit', function (event_handler) {     event_handler.preventdefault();     runbeforesubmit(); <------------------------ shared code process before submit  });   // if loses focus  form.addeventlistener('blur', function (event) {    runbeforesubmit(); <------------------------ shared code process before submit    form.submit();    return false; }, true); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -