Home All Groups Group Topic Archive Search About
Author
7 Jul 2009 1:00 AM
Andy
hi,

I'm trying to detect changes in a textbox to set a flag in
javascript.  I hook into the text boxes keypress event, which works
fine except when you press the backspace key.  In this case, the
keypress event is not raised, and my flag never gets set.

Any ideas?

Thanks
Andy

Author
7 Jul 2009 1:02 AM
Andy
On Jul 6, 9:00 pm, Andy <ajj3***@alum.rit.edu> wrote:
> I'm trying to detect changes in a textbox to set a flag in
> javascript.  I hook into the text boxes keypress event, which works
> fine except when you press the backspace key.  In this case, the
> keypress event is not raised, and my flag never gets set.

Oh, I also tired the change event, but that doesn't fire until after
the control loses focus.

Thanks
Are all your drivers up to date? click for free checkup

Author
7 Jul 2009 7:09 AM
Joy
Hi Andy,

According to me "onkeyup" event will work for you. But the downside is that
it will be fired too many times. However you can detach the event after the
first KeyUp with something like following:

<script type="text/javascript">

function checkChanged( tfValue )
{
alert( "In function" );
document.getElementById( "hdChanged" ).value = "true";
tfValue.setAttribute( "onkeyup", "" );
}

</script>

<form id="frmMain" action="test.html">
<input type="text" onkeyup="checkChanged( this );" />
<input type="hidden" id="hdChanged" name="hdChanged"
value="false" />
</form>


If you are looking to set the flag only when a specific Key is pressed then
do so by comparing the value of "event.keyCode" with that specific Key's Key
Code.

For Javascript Char Codes visit the following:
http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx


Please let me know if it helps.


regards,
Joy

Show quoteHide quote
"Andy" wrote:

> On Jul 6, 9:00 pm, Andy <ajj3***@alum.rit.edu> wrote:
> > I'm trying to detect changes in a textbox to set a flag in
> > javascript.  I hook into the text boxes keypress event, which works
> > fine except when you press the backspace key.  In this case, the
> > keypress event is not raised, and my flag never gets set.
>
> Oh, I also tired the change event, but that doesn't fire until after
> the control loses focus.
>
> Thanks
>
Author
7 Jul 2009 9:44 AM
Mark Rae [MVP]
"Joy" <J**@discussions.microsoft.com> wrote in message
news:5EC950CF-A6FE-422F-B464-3C099E83D449@microsoft.com...

> If you are looking to set the flag only when a specific Key is pressed
> then
> do so by comparing the value of "event.keyCode" with that specific Key's
> Key
> Code.

This will work only in IE. For cross-browser compatibility, you will need
something like:

var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13)    // Enter key
{
    // do something
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Bookmark and Share