Javascript question

A

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
 
A

Andy

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
 
J

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/70...306e3e25/Javascript-Char-Codes-Key-Codes.aspx


Please let me know if it helps.


regards,
Joy
 
A

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:

Hi,

Thanks for the tip. I'm not sure what you mean by 'fired too many
times.' In my particular case, I don't care which key was pressed,
only that a change was made to the previous value. I don't even care
if they eventually type the orginal value back in, I consider the form
"dirty" and enable the save button.

Unless you see a problem, it sounds like onkeyup is the way to go...

Thanks!
Andy
 

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

Top