focus on first feld using jquery validator -


i using jquery validator validate form fields. below fields:

enter image description here

  1. focus @ third field i.e email 2 want focus on first field i.e fax.

  2. error message displayed between text both , other static text due alignment shifts here , there. best possible way display messages.

  3. in few scenarios error message displayed not focus due user not aware of error.

code:

var validator = $("#enrollmentform").validate({     errorelement: 'label',     errorplacement: function(error, element) {         error.insertafter(element);         element.focus();     }, }); 

can provide solution?

your own code causing problem you're describing.

 errorplacement: function (error, element) {      error.insertafter(element);      element.focus();  // <- line causing whole problem  }, 

by using element.focus within errorplacement, you're telling focus on element being evaluated right when it's been evaluated. if mutiple elements invalid @ once, focus naturally left on last 1 evaluated, since form evaluated top bottom.

demo of problem caused own code: http://jsfiddle.net/6y3k6jtc/


removing element.focus() line totally fixes code, because you're allowing plugin's default focus operate intended. (by default, plugin automatically knows place focus on first invalid input element.)

 errorplacement: function (error, element) {      error.insertafter(element);  }, 

demo of working code: http://jsfiddle.net/6y3k6jtc/1/


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -