...

View Full Version : onchange focus() issue



Kakao
05-03-2006, 09:13 PM
When I trigger the onchange event clicking on the other input field the focus() and select() methods (used alone or combined) in the validate() function works, but when I use the tab key to trigger the onchange event they does not work. Why? What can be done? Using FF 1.0.8



<html>
<script type="text/javascript">
function validate(obj) {
if (obj.value > 2) {
alert('must be <= 2');
obj.focus();
obj.select();
}
}
</script>
<body>
<input type="text" id="a" onchange="validate(this);" value="1">
<input type="text" id="b" onchange="validate(this);" value="2">
</body>
</html>


Regards, Kakao

Works in Opera 8.52

Kakao
05-11-2006, 03:14 PM
I found the solution. The problem is that the onchange event is handled first and then when the event returns the tab key press is handled and the focus go to the next element. The solution:

<html>
<script type="text/javascript">
function validate(obj, value) {
if (obj.value > 2) {
alert('must be <= 2 ' + obj.id);
setTimeout(function () { obj.focus() }, 50);
obj.value = value;
obj.select();
}
}
</script>
<body>
<input type="text" id="a" onchange="validate(this, 1);" value="1">
<input type="text" id="b" onchange="validate(this, 2);" value="2">
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum