View Full Version : onChange not firing
landon11
02-03-2003, 03:49 PM
I have a function like this:
function myOnchange(myEl){
myEl.value=(myEl.value<20)?20:myEl.value;
}
If I put 5 into the textbox it changes to 20 but if I put 5 in a second time it does not.
beetle
02-03-2003, 04:08 PM
The onchange event for textfields won't fire until you blur from the object.
landon11
02-03-2003, 04:13 PM
Thank you, but I do blur.
1. I type 5 then hit tab and the 5 is changed to 20
2. I type 5 then hit tab and the 5 stays 5.
beetle
02-03-2003, 04:16 PM
Can I see all the code?
landon11
02-03-2003, 04:20 PM
<html>
<head>
<script>
function myOnchange(myEl){
myEl.value=(myEl.value<20)?20:myEl.value;
}
</script>
</head>
<body>
<input name="txt1" onChange="myOnchange(this);">
</body>
</html>
beetle
02-03-2003, 04:31 PM
Well, after some tests, it appears that this is what happens
Initial value is ''. The browser uses this to determine a change for onchange.
You type in '5'.
After commiting the change by leaving the object, the browser compares '' to '5' and recognizes a change, firing the onchange event
The browser now recognizes the compare value as '5'
Your function changes the value to '20', no new compare value is set
You change it to '5' again, but the browser still thinks the compare value is '5', so the onchange never firesIf you try typing '5', letting it change then '4' or something besides '5', then it works.
So, live with that, or use onBlur instead.
It seems it saves the last user input and compares it with the new user input, instead of comparing this last one with the script changed input.
You can get the results you want by using the onblur though:
<input onblur=myOnchange(this) name="textField" type="text" size="30">
Values also change when the text field loses focus, as happened with onchange
PS: just sent an email to M$ about this.
landon11
02-03-2003, 04:55 PM
Thank you both, but I can not use onBlur because the textbox is used for an age. I have a date of birth textbox and if someone puts the date of birth then I calculate the age and put it in the age textbox but if you input the age then I calculate the date of birth as being such as: if you put 25 as age the DOB would be 02/03/1978.
So my problem with onblur is that if I put 01/03/1978 then tab to age textbox which is 25 then tab off it will change my DOB to 02/03/1978.
Thanks anyway.
Ok, let's see something. I'm 20 now, so I was born 20 years ago and some months and some days. By knowing my age in years only, you can NEVER know my exact birthdate. I could have been born 20 years and 1 day ago, 20 years and 200 days ago, 20 years and 1 minute ago. You never know =).
So my opinion is you should think about just asking the users for their birthdate and calculate their age from there, but not doing the reverse, as it will never be accurate.
You can't even "always guess" the year the person was born in. I could have been born in November 1982 or January 1983 =).
This was just a little thought, hope it helps.
landon11
02-03-2003, 05:30 PM
Thanks I agree with you, but I have no say so.
whammy
02-04-2003, 03:22 PM
If you're doing the programming, you should always have "say-so".
Just tell it like it is! - "You can calculate their age by their birthdate, but you can't calculate their birthdate by their age (unless they were to put in their age including the days, hours, minutes, and seconds as well)".
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.