Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  77] [ 6]  / answers: 1 / hits: 31982  / 13 Years ago, mon, august 29, 2011, 12:00:00

I have something like:



<input type=textbox id=partNumber onChange=validatePart(this);>
<input type=textbox id=quantity onfocus=saveOldQuantity(this);>


The onChange event is properly firing after a change is made and the textbox loses focus. When quantity gains focus, the onfocus event is properly firing to save the old value of quantity before changes are made.



If validatePart() detects an invalid partNumber value, it alerts the user to that fact. After the alert() has cleared, I'd like to return focus to the partNumber input. But doing a focus() on the input node is not giving it focus. Debugging is tricky here, because the IE debug window interaction is, of course, changing the focus.



How can I ensure focus returns to partNumber if an error is detected in validatePart()?



EDIT:
A simple version of validatePart():



function validatePart(node) {
if (!node.value) {
alert('Please enter a part number.');
node.focus(); // <======= why isn't this having any effect??
}
}

More From » html

 Answers
3

Adding a small timeout and resetting the focus back to the partNumber textbox should do the trick.



Thus your validatePart() function becomes something like:



function validatePart(node) {
if (!node.value) {
alert('Please enter a part number.');
setTimeout(function(){node.focus();}, 1);
}
}


Here's a quick live example that simply fires an alert no matter what is entered into the partNumber textbox and successfully returns focus back to the partNumber textbox (even if you tab off it to the quantity textbox.


[#90360] Friday, August 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;