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
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.