Perform Button Click Event When User Press Enter Key in Textbox

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



Leave a reply



Submit