2

What I have

I have a simple view in my .cshtml file which uses the form

                      //actionName  //controller //method         //htmlAttributes
@using (Html.BeginForm("TableName", "Data",      FormMethod.Post, new { id = "frmName" }))
{
    //combobox with id
    //button with id
}

I was able to implement triggering the button with Enter key by using answers from this thread as a basis.

$('#frmName').on('keypress', function (e) {
  sender = event || window.event;
  var hasfocus = $('#btnGo').is(':focus') || false;
  if (sender.keyCode == 13 && !hasfocus) {
     e.preventDefault();
     $('#btnGo').trigger('click');
  }
});

In above scenario, everything works as intended.

What I need

I recently found that this particular view will not be using the form. As a result, I wanted to adapt my javascript code to separate it from the frmName. However, it proved to be trickier than I anticipated.

What I tried

I tried replacing the formID with the button ID

$('#btnGo').on('keypress', function (e) {
  sender = event || window.event;
  var hasfocus = $('#btnGo').is(':focus') || false;
  if (sender.keyCode == 13 && !hasfocus) {
     e.preventDefault();
     $('#btnGo').trigger('click');
  }
});

I tried implementing something similar to accepted answer of the thread mentioned above.

document.getElementById("cbID")
.addEventListener("keyup", function (event) {
   event.preventDefault();
   if (event.keyCode == 13 || $('#btnGo').is(':focus')) {
      document.getElementById("btnGo").click();
   }
});

In both cases, when I select a value from the combobox and press enter key, an unexpected error pops up

Result of attempts above

Can you please point me in the right direction how I can attach Enter to button while separating it from my FormID? (Preferably, without altering my initial javascript code too much, but I'm flexible to suggestions)

Edit

After testing in different browsers(IE, Chrome, FF) for debugging purposes, I got the following results:

  • Initial scenario works as intended in all 3 browsers
  • My attempts inside Chrome leads to an error inside the browser instead of intended error checker
  • My attempts inside FF leads so asking for my credentials, which I am hesitant to give

Honestly, I am more interested now in why pressing Enter leads me to some kind of file saving and options provided in my picture. The question itself is still relevant though.

Community
  • 1
  • 1
Vadzim Savenok
  • 930
  • 3
  • 14
  • 37
  • Try in chrome/firefox and inspect the browser console. – Shyju Aug 26 '16 at 19:37
  • @Shyju Unfortunately, the customer only uses IE, so other browsers are out of option here... – Vadzim Savenok Aug 26 '16 at 19:45
  • For your debugging, can't you test with other browsers ? Just don't tell the customer that you used a non IE browser to fix the issue – Shyju Aug 26 '16 at 19:46
  • @Shyju I have already tested in other browsers for pure debugging. However, in Chrome, everything proceeds with a normal logic, while Firefox keeps asking for credentials, which I am hesitant to ask. As a result, I do not understand, what is so particular about IE, that the same action provides the result from the picture. Mind you, My initial scenario works as intended in all browsers. – Vadzim Savenok Aug 26 '16 at 20:02

0 Answers0