issue with click on button

G

graphicsxp

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 javascript works fine. If I'm in txtJournalist2 and I press enter,
the OnClick Server event of btnAuthor2 is triggered. But the problem is
that for some reason, the OnClick Server event of btnAuthor is also
triggered afterward (not from the javascript as no alert popup is
shown).

In fact if I invert the place of the buttons in the page, so that
btnAuthor2 is BEFORE btnAuthor then there is always a call to the
OnClick event of btnAuthor2 even if I was in the first textbox when I
pressed Enter......

Does anyone know why ?

Thanks



<asp:TextBox ID="txtJournalist" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor" runat="server" Text="Search..."
CausesValidation="False" />

<asp:TextBox ID="txtJournalist2" runat="server" onkeypress="fct(event,
this);"></asp:TextBox>
<asp:Button ID="btnAuthor2" runat="server" Text="Search..."
CausesValidation="False" />&nbsp;




function fct(evt, obj)
{
evt = (evt)? evt : event
var charCode = (evt.which) ? evt.which : evt.keyCode
if(charCode == 13)
{
if (obj.name == 'ctl00$cpBody$txtJournalist')
{
alert('ctl00$cpBody$txtJournalist');
document.getElementById("ctl00_cpBody_btnAuthor").click();
}
else if (obj.name == 'ctl00$cpBody$txtJournalist2')
{
alert('ctl00$cpBody$txtJournalist2');
document.getElementById("ctl00_cpBody_btnAuthor2").click();
}
else if (obj.name == 'ctl00$cpBody$txtPublication')
{
alert('ctl00$cpBody$txtPublication');

document.getElementById("ctl00_cpBody_btnPublication").click();
}
return false;
}
else
return true;
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

If you press enter, the first button in the form is used to post the
form. As the form is already being posted, the event sent by the click()
method is never handled.
 
G

graphicsxp

Fair enough, but then what is the workaround ?
Göran Andersson said:
If you press enter, the first button in the form is used to post the
form. As the form is already being posted, the event sent by the click()
method is never handled.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Make sure that the keypress event is stopped from posting the form. IIRC
you use cancelBubble to do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top