Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onChange event

Status
Not open for further replies.

tsdragon

Programmer
Dec 18, 2000
5,133
US
I just discovered that the onChange event is not always called correctly, at least in IE 5.5. If you change a text field which has an onChange event handler, and then click on the submit button without moving the cursor the onChange event handler may not be called.

How you work around this depends on what you do in the onChange event handler, so I won't go into it. Just a heads-up. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
why don't you use the "onBlur" event handler, it will solve your problem ;-)

Hope this helps ...
 
onchange is only called when the field's value is changed by the user (read: not programatically) AND the element loses focus. jaredn@eae.net -
 
I didn't want to use onBlur because I only want to call the function when the value is actually CHANGED. If I use onBlur I have to store the initial value of the field and check it against the current value to see if it's changed. Not a major problem, except when you have a number of fields it can slow things down.

In the situation I found where onChange wasn't being called the field WAS changed by the user (me) and DID lose focus when I clicked on the submit button, but the onChange event was not called before the submit action took place.

Obviously this is a bug in the event handling sequence. I'm not even sure in this case that an onBlur event handler would get called before the submit action. Maybe I'll test that out later today. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
tsdragon,

Try the following example. It will list the two events in the order they are received:

<form action=&quot;javascript:void(benluc.innerHTML+='<br>Hi. My names form submission!')&quot;>
<input type=&quot;text&quot; onchange=&quot;benluc.innerHTML+='<br>Hi. My names onchange!'&quot;>
<input type=&quot;submit&quot;>
</form>
<div id=&quot;benluc&quot;>

</div>
jared@eae.net -
 
I agree that your example shows the onChange event being called before submit. However, I know I saw the onChange event not being called properly at one point in testing one of my forms. I can't even seem to be able to reproduce the error now, so perhaps it was just a fluke, but I watched it happen several times in a row. Any ideas?
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
What was the context of the problem? Were you alerting something, changing a variable? jared@eae.net -
 
What I was doing was:
If the user changed the mailing address, call a function that checks the &quot;shipping address same as mailing address&quot; checkbox, and if it's checked, set the shipping address to the new value of the mailing address.

I would change part of the mailing address, click the submit button, and the shipping address would not get changed.

But... like I said, I can't even duplicate the problem now.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Maybe it was just a silly error you missed. As js is single threaded, it will wait to process the next event until one is done. jared@eae.net -
 
Try '[tt]onPropertyChange[/tt]'.

This handler will fire immediately upon modification of the tag to which it has been defined.

This is probably only IE-only. Haven't bothered testing this in Nutscr... I mean Netscape.
 
Thanks for the hint, JCP. I never heard of onPropertyChange before. I'll check it out.

As for not working in NS, I don't care. I gave up trying to make things work in THAT over a year ago. If NS is so great, and it's owned by AOL, then why is IE still AOL's default browser? I think AOL is trying to kill it.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top