Perform Button click event when user press Enter key in Textbox
In the aspx page load event, add an onkeypress
to the box.
this.TextBox1.Attributes.Add(
"onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
Then add this javascript to evaluate the key press, and if it is "enter," click the right button.
<script>
function button_click(objTextBox,objBtnID)
{
if(window.event.keyCode==13)
{
document.getElementById(objBtnID).focus();
document.getElementById(objBtnID).click();
}
}
</script>
Trigger a button click with JavaScript on the Enter key in a text box
In jQuery, the following would work:
$("#id_of_textbox").keyup(function(event) {
if (event.keyCode === 13) {
$("#id_of_button").click();
}
});
$("#pw").keyup(function(event) { if (event.keyCode === 13) { $("#myButton").click(); }});
$("#myButton").click(function() { alert("Button code executed.");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Username:<input id="username" type="text"><br>Password: <input id="pw" type="password"><br><button id="myButton">Submit</button>
Fire event on enter key press for a textbox
ASPX:
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
JS:
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueID%>', "");
}
}
CS:
protected void Button1_Click1(object sender, EventArgs e)
{
}
Press enter in textbox to and execute button command
You could register to the KeyDown-Event of the Textbox, look if the pressed key is Enter and then execute the EventHandler of the button:
private void buttonTest_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
private void textBoxTest_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonTest_Click(this, new EventArgs());
}
}
Click event triggered by enter key
<input type=submit>
in the form gets the automatic Enter Key behavior. This is known as an implicit form submission.
How to trigger click event when pressing enter key? Text area
Although you can use the onkeydown
attribute I prefer to do these things through JavaScript event listeners & handlers. If you want to do this with the onkeydown attribute then look at Bryan's answer.
I would first add an ID/Class name to the input so we can easily target it. In my example i will add searchText
as the ID for this input.
JavaScript:
document.getElementById('searchText').addEventListener("keydown",function(e){
if(e.keyCode == 13){
//doSomething()
}
});
jQuery:
$('#searchText').on('keydown',function(e){
if(e.which == '13'){
//doSomething();
}
});
Button click event being fired automatically during TextBoxFor Enter key press
If you want to disable your press enter in your keyboard, try this:
$(function () {
//On your document ready function, add this:
$('html').bind('keypress', function (e) {
if (e.keyCode == 13) {
return false;
}
});
}
In aspx how to trigger button click event when press Enter on a textbox while the button click event defined in source cs file
If you use Panel
you would not need to use any javascript function. You can specify default button
Id for panel like below
<asp:Panel runat="server" DefaultButton="btnSearchSuiteGroup">
<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD">
</asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search" CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
</asp:Button>
</asp:Panel>
OfCourse you can have Multiple panels
on single page for assigning different default buttons
for separate panels
!
For More On Panel.DefaultButton Property
Allow button click function on the Enter Key in Textbox
You're trying with keyup
, but the keyup event is sent to an element when the user releases a key on the keyboard.
You could use keypress
, where the keypress event is sent to an element when the browser registers keyboard input.
$("#SearchNo").keypress(function(event){ if(event.keyCode == 13){ alert('You pressed enter!'); $("#btnSearch").click(); }});$("#btnSearch").click(function() { alert("click"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" id="SearchNo">
<input type="button" id="btnSearch" value="Search" />
Related Topics
Non-Virtual Interface Design Pattern in C#/C++
System.Data.Sqlclient.Sqlexception: Invalid Column Name 'Phone_Types_Phone_Type_Id'
C# Null Coalescing Operator Equivalent for C++
Incorrect Syntax Near the Keyword 'User'
How to Add a Reference to an Unmanaged C++ Project Called by a C# Project
Load Different CSS File Based on Browser
Error While Using Executenonquery C#
How to Get Float Value with SQLdatareader
What Is Difference Between Regasm.Exe and Regsvr32? How to Generate a Tlb File Using Regsvr32
SQL Injections with Replace Single-Quotation and Validate Integers
Does Ef Upsert Have to Be Done Manually
How to Convert Code from C# to PHP
C++, C# and JavaScript on Winrt
Insert Text into the Textbox of Another Application
Mono on Debian: Could Not Find File "/Srv/Www/Proj/Bin\Roslyn\Csc.Exe"