Capture when a textbox has changed!

H

Hans

Hi!

Does anybody of you have a good algorithm to capture when a value in a
textbox is changed? I'm using the keypress/keydown events but they seems to
be a bit inconsistent (IE and Mozilla). Basically what I want is to set is a
"isDirty" flag when a user has changed any value in the textbox (or in a
dropdown) and then if he/she tries to leave the record without saving I want
to give a save warning. Since I don't want set the isdirty flag if you for
example klick tab, certain F-buttons, arrows (up/down/left/right) etc I
tried to capture the keypress/keydown event and ignore specific keycodes.
Since there are several keycodes that should be ignored I was hoping there
is a better solution.

Must support IE6 and NN7+ (mozilla). Since the site is unicode compatible
the user may add chinese characters for example.

Regards
/Hans
 
H

harry

use the onChange() event mate, trying to grab key press's is a bit of a
nightmare as key's like "del", "backspace" behave differently!
 
C

Colin McKinnon

Hans said:
Hi!

Does anybody of you have a good algorithm to capture when a value in a
textbox is changed? I'm using the keypress/keydown events but they seems
<snip>

Most form elements have a defaultValue property (what the value would be
changed to if the form was reset).
Must support IE6 and NN7+ (mozilla). Since the site is unicode compatible
the user may add chinese characters for example.

Note that the defaultValue property doesn't work properly for hidden input
elements in Mozilla based browsers.

HTH

C.
 
H

Hans

Thanks I tested the onchange event but it did not fire but I now realize
that it fires when the control loses the focus and not for every keystroke.

Thanks
/Hans
 
H

Hans

Hi again. The onchange event does not seem to work if I inside a textbox for
example capture F12 which is setting the value in the textbox to some value.
When I tab to the next field the onchange event do not fire (i can of course
set the isdirty flag in the code that is capturing the return key). If I
type a value in the first textbox and then tab to the second the onchange
event fire. Is it something strange with my code of capturing the F12 button
or is the onchange not triggered if the value is changed by javascript code?


<html>
<body>
<script language="javascript" type="text/javascript">
function captureF12(evt)
{
var keyCode = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode :
evt.which;
if (keyCode==123)
document.getElementById('a').value='Test';
return true;
}
</script>

<form>
<input type="text" id="a" onchange="alert('onchange has fired');"
onkeydown="captureF12(event);">
<input type="text" id="b">
</form>
</body>
</html>

Regards
/Hans
 
M

Michael Winter

Hi again. The onchange event does not seem to work if I inside a textbox
for example capture F12 which is setting the value in the textbox to
some value.

That's because F12 doesn't change the value.

On my browser (Opera), F12 brings up the Quick Preferences menu. I don't
think it does anything in IE or Mozilla.

Why do you think F12 is significant? None of the function keys should be.

[snip]
<script language="javascript" type="text/javascript">

The language attribute is deprecated and redundant. Don't use it. Just
stick with the (required) type attribute.

[snip]
 
H

Hans

That's because F12 doesn't change the value.
No I understand that it does not change the value but in my case I capture
the event and change the value by javascript. But obviously the onchange
event is not triggered.
On my browser (Opera), F12 brings up the Quick Preferences menu. I don't
think it does anything in IE or Mozilla.
Yes that is why we use F12 to insert the username/timestamp in a field (from
the windows version of the application users are used to use F5 but that is
not a good key to use since it is used by many browsers to reload the page).
Why do you think F12 is significant? None of the function keys should be.

[snip]
<script language="javascript" type="text/javascript">

The language attribute is deprecated and redundant. Don't use it. Just
stick with the (required) type attribute.
Yes I know. We supported NN4.7x before this release so I had to stick with
the old language attribute.

Once again thanks for your help.

regards
/Hans
 

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
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top