PDA

View Full Version : problem with onChange event in IE5.0 ,IE6.0


learner
08-09-2002, 04:30 AM
hi ,


The following code for removing space IF IT IS THE first character works fine in Netscape ,but not in IE.For the first change it works in IE but immediatly if we change again,ie if space is given followed by text onchange event is not firing IE.I dont want to use onBlur because there are many text fields and if i give onBlur in all text fields alerts are coming in a loop.So i have to use onChange.
Here is the code,



<html>
<head>
<script>
function CheckIt(X,ABC){
a=0;
FL=ABC.slice(0,1);
LL=ABC.slice(ABC.length-1,ABC.length);
if(FL==" "){ABC=ABC.slice(1,ABC.length);a=1;}
if(LL==" "){ABC=ABC.slice(0,ABC.length-1);a=1;}
X.value=ABC;
if(a!=0){CheckIt(X,ABC)}
}
</script>
</head>
<body>
<form name="FormName">
<input type="text" onchange="CheckIt(this,this.value)" name="AName" size="12">
</form>
</body>
</html>




Thanks
rama

adios
08-09-2002, 04:50 AM
Never heard that one. Try this:

<html>
<head>
<script>
function trimHead(field) {
while (field.value.charAt(0) == ' ') field.value = field.value.substring(1);
}
</script>
</head>
<body>
<form name="FormName">
<input type="text" size="12" onchange="trimHead(this)">
</form>
</body>
</html>

beetle
08-09-2002, 08:05 AM
Isn't this easier?onChange="this.value=this.value.replace(/^[\s]/,'')"Or, to removing all leading spaces...onChange="this.value=this.value.replace(/^[\s]*/,'')"Could be a function too<html>
<head>
<script>
function stripSpace(obj)
{
var text = obj.value.replace(/^[\s]/,'');
obj.value = text;
}
</script>
</head>
<body>
<form name="FormName">
<input type="text" size="12" onChange="stripSpace(this)">
</form>
</body>
</html>

learner
08-10-2002, 07:28 AM
Hi,

The functions to trim space or remove space works fine.There is absolutely no problem.

But the real problem is with the onChange Event handler is Internrt Explorer 5.0 and 6.0.


Very first time when the screen is loaded the user enters

a space followed by some characters.when the focus goes to the next field the space is trimmed and only the characters he typed remains.

Immediatly when the users places the cursor in the textbox

and gives a space in front of the characters just one space,and when the tab is pressed,the onChange event is not getting fired and the space is NOT removed.But after that u again give space ,it is trimed.


Is it a bug in IE5.0,6.0.Please correct me if i am wrong or if this is a problem is there any work around.

Thanks
learner

jkd
08-10-2002, 07:36 AM
beetle, no need for the brackets in /^[\s]*/, there is only one character you are matching (whitespace), so /^\s*/ works just fine.

learner
08-16-2002, 05:52 PM
Hi,

The functions to trim space or remove space works fine.There is absolutely no problem.

But the real problem is with the onChange Event handler is Internrt Explorer 5.0 and 6.0.


Very first time when the screen is loaded the user enters

a space followed by some characters.when the focus goes to the next field the space is trimmed and only the characters he typed remains.

Immediatly when the users places the cursor in the textbox

and gives a space in front of the characters just one space,and when the tab is pressed,the onChange event is not getting fired and the space is NOT removed.But after that u again give space ,it is trimed.


Is it a bug in IE5.0,6.0.Please correct me if i am wrong or if this is a problem is there any work around.

Thanks
learner

beetle
08-16-2002, 06:11 PM
Add this to the inputonKeyDown="if (event.keyCode == 32) stripSpace(this)"

learner
08-17-2002, 11:45 AM
Hi,

The code works fine and doesnt allow me to type space as the first character.


But as i said earlier is there any problem with onChange event in IE5.0

But as isaid earlier is there any problem with onChange event in IE5.0 AND IE6.0

beetle
08-17-2002, 02:16 PM
Uh. Whoa. I know what you said, that is why I offered this '2nd line of defense'. The best event for this action you have already dismissed for specific reasons, onBlur().

Cheers

sunil_chaudhari
09-11-2008, 07:44 AM
Uh. Whoa. I know what you said, that is why I offered this '2nd line of defense'. The best event for this action you have already dismissed for specific reasons, onBlur().

Cheers
But onblur will take more processing time if have to do some calculations based on the value which in not even changed??

Philip M
09-11-2008, 04:42 PM
As far as I know, the problem is that Internet Explorer recognizes something changed just after you lose focus on that element. That is, it "thinks" you're still changing it and will wait until you click somewhere on the side or on another element.

This problem is a quite well-known IE bug (sorry, feature), and you will find an explanation and the solution at:-

http://ewbi.blogs.com/develops/2004/12/ie_changing_a_t.html

The same (or a related) bug in IE requires a delay in re-focusing, for example after a failed validation:-


<script type = "text/javascript">
function fieldFocus() {
document.forms[0].username.focus();
}
setTimeout(fieldFocus, 10); // 10ms delay
</script>

Or try:-

onkeyup = "this.value=this.value.replace(/^\s/,'')"


I have the idea that there is a bug in Internet Explorer (all versions, as far as I know) whereby the onChange event does not fire if the user makes use of auto-completion.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.