472,787 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

Wrong button.click function is trigger when press Enter key on textbox

18
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of two text boxes and two button. When ever a key is press on textbox one, I want to trigger button1.click, and press on text box 2 to trigger button2.click. The id and button is correct from the alert but i don't know why it keep calling button1.click event when I press on textbox 2.

<script type="text/javascript">

//Handle enter key press to trigger button click
function focusNext(objEvent, strElement)
{
if ( objEvent.keyCode == 13)
{
oNextObj = document.getElementById(strElement);
alert("Enter key is pressed!!");
if( oNextObj ) {
oNextObj.focus();
// alert("Focused on Object and before click() trigger");
oNextObj.click();
alert("processed clicked on ID: " + strElement + " Value: " + oNextObj.value);

} //end if

return;

} //end if

} //end function

</script>
.
.
.
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button 1" /><br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button 2" /></div>
</form>
</body>

-- vb code


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "Text will display here<br>Click on the text box then press enter key to see change"
Me.TextBox1.Text = "Textbox 1"
Me.TextBox2.Text = "Textbox 2"

Me.TextBox1.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button1.ClientID & "')")
Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ClientID & "')")
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Text = "Enter key is pressed from Textbox 2"
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Enter key is pressed from Textbox 1"
End Sub

End Class
Apr 7 '08 #1
1 6775
shweta123
692 Expert 512MB
Hi,

May be you are sending the wrong Id for button while calling javascript function. You can try using ID instead of ClientID.
e.g

Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ID & "')")



I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the page has multiple button such login page with a search function somewhere around, then it's not respond properly. I have attached a brief example of two text boxes and two button. When ever a key is press on textbox one, I want to trigger button1.click, and press on text box 2 to trigger button2.click. The id and button is correct from the alert but i don't know why it keep calling button1.click event when I press on textbox 2.

<script type="text/javascript">

//Handle enter key press to trigger button click
function focusNext(objEvent, strElement)
{
if ( objEvent.keyCode == 13)
{
oNextObj = document.getElementById(strElement);
alert("Enter key is pressed!!");
if( oNextObj ) {
oNextObj.focus();
// alert("Focused on Object and before click() trigger");
oNextObj.click();
alert("processed clicked on ID: " + strElement + " Value: " + oNextObj.value);

} //end if

return;

} //end if

} //end function

</script>
.
.
.
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button 1" /><br />
<br />
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button 2" /></div>
</form>
</body>

-- vb code


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Label1.Text = "Text will display here<br>Click on the text box then press enter key to see change"
Me.TextBox1.Text = "Textbox 1"
Me.TextBox2.Text = "Textbox 2"

Me.TextBox1.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button1.ClientID & "')")
Me.TextBox2.Attributes.Add("onkeydown", "javascript:focusNext(event,'" & Me.Button2.ClientID & "')")
End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Text = "Enter key is pressed from Textbox 2"
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Enter key is pressed from Textbox 1"
End Sub

End Class
Apr 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Miquel | last post by:
Hi all. I felt frustrated when developing an 'UserControl' derivated from textBox, because sequence event (and Validate event) seems to fail. I Always thought my code was wrong. But after...
1
by: Raghuvansh | last post by:
I have the following simple code of two panels and two pairs of textboxes and buttons. <form id="Form1" method="post" runat="server"> <asp:Panel id="Panel1" runat="server" Width="272px"...
5
by: Steve | last post by:
Hi, Is it possible to make hitting the enter key in an ASP textbox run the code behind an ASP button on a form? I have a search page which users tend to type in the query then just hit enter...
5
by: Girish | last post by:
I have TWO submit buttons of type IMAGE on my asp form. This renders as <input type="image">. I need to be able to eble the ENTER button for both buttons. Yes, I know that for the input type...
2
by: Lenard Gunda | last post by:
Hi! I have an ASP.NET page with multiple TextBox fields and buttons. When I press the <enter> key in a TextBox, the page postbacks, and one of the buttons get fired (OnClick). Now certain...
5
by: TS | last post by:
for some reason, it posts to the server, but no click events of any buttons on form fire. the button is the first one on the form. when the focus is inside the textbox, it doesnt' work. if i click...
2
by: Lenster | last post by:
Environment --------------- Visual Studio.NET 2003 Version 7.1.3088 ..NET Framework 1.1 Version 1.1.4322 SP1 XP Professional 5.1.2600 SP2 Build 2600 Problem Description...
3
by: graphicsxp | last post by:
Hi, I've written some javascript function so that if a textbox has the focus and the user press enter, it triggers a click on a button 'associated' to this textbox (see code at the end) The...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.