Tuesday, October 7, 2014

Go to Next input of Enter click also disable Submit on Enter Click for Other than Submit-Button Click

Following is the Javascript Code to Disable default submit on controls other than button and rather go to next control on "enter click"

and also the focus will only got the controls which are not disabled, hidden or readonly.

This is very useful to get desktop like functionality in web pages where mouse should be used less or not used, improving user experience and performance of the page.

 <script type="text/javascript">
 function disablesubmitandgotonext() {
 var ctrls = $("input:not(:hidden):not([readonly]):not([disabled]),radio:not([readonly]):not([disabled]),text:not([readonly]):not([disabled])");
 ctrls.each(function (i, elem) {
 var $input = $(this);
 $input.keydown(function (event) {
 if (event.keyCode == 13 && $(this).attr('type') != 'submit') {
 event.preventDefault();
 ctrls.eq((i + 1)).focus(200);
 return false;
 }
 return true;
 });
 });
 }

 //Go to Next input of Enter click also disable Submit on Enter Click for Other than Submit-Button Click
 $(
 function () {
// this is to focus on first contron while pageload

 $("#ContentPlaceHolder1_ddlAssetGroupID_chosen .chosen-single").focus();

disablesubmitandgotonext();
 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
 }
 );
 });
 </script>