Reputation: 9151
I am trying to implement a textbox which when press on enter on keyboard but will not automatically submit the form. This textbox is inside a form:
@Html.TextBoxFor(model => model.ID, new { @class = "form-control
input-sm", placeholder = "Enter Id",
onkeypress = "return fncValidateMemberID(event);" })
This is the Javascript code:
function fncValidateMemberID(e) {
if (e.keyCode == 13) {
//My AJAX logic here but I cannot get the text value I inputted on my textbox
return false;
}
return true;
}
The problem with my Javascript code is I cannot get text value.
Upvotes: 0
Views: 103
Reputation: 5252
Please try the following:
function fncValidateMemberID(e) {
if (e.keyCode == 13) {
alert($("#ID").val());
return false;
}
return true;
}
It should alert the value of your text input.
Upvotes: 1
Reputation: 65815
Since you've tagged the question with jQuery, you can get the value of the text field with the .val()
method, as in the following (replace ID
with your textbox's clientID
):
var text = $("#ID").val();
Or, without jQuery (vanilla JavaScript):
var text = document.getElementById("ID").value;
Upvotes: 1
Reputation: 56716
Pass the element to your handler function, and then you'll be able to get its value:
onkeypress = "return fncValidateMemberID(event, this);"
function fncValidateMemberID(e, textbox) {
if (e.keyCode == 13) {
var text = textbox.value;
...
Upvotes: 1