By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,312 Members | 2,835 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,312 IT Pros & Developers. It's quick & easy.

Capture when a textbox has changed!

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Wed, 29 Sep 2004 12:12:21 +0200, Hans <so***@nospam.com> wrote:
Does anybody of you have a good algorithm to capture when a value in a
textbox is changed?


Umm, the change event, perhaps?

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
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!
"Hans" <so***@nospam.com> wrote in message
news:cj**********@green.tninet.se...
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

Jul 23 '05 #3

P: n/a
Hans wrote:
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.
Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

P: n/a
On Wed, 29 Sep 2004 14:01:08 +0200, Hans <so***@nospam.com> wrote:
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]
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

P: n/a
> 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
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.