Welcome to WebmasterWorld Guest from 50.19.57.50

Forum Moderators: open

Message Too Old, No Replies

focus() in firefox not working properly

     
8:25 pm on Dec 6, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 29, 2004
posts:49
votes: 0


I have this javascript code that checks to see if only numbers (and comma's and period's) are present in an html input field, but focus() isn't working in firefox.

function checkNumOnly(thisField) {
if (/[^0-9,.]/.test(thisField.value)) {
thisField.style.backgroundColor='#ff6666';
alert("Du skrev:\n" + thisField.value + "\n\nmen kun tall er tillatt.");
thisField.focus();
}
}

when i tab out of the input field (and i have letters or something there to trigger the alert) the cursor doesn't get put back in the input field, it goes to the next thing on the page:

Text2 <input type="text" name="txtfld2" value="" onblur="checkNumOnly(this);" />
1:56 pm on Dec 7, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


Firefox, on Linux anyway, has some issues with focus, though I hadn't encountered this before. See [bugzilla.mozilla.org...]

You could try something like this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
</style>
<script type="text/javascript">
</script>
</head>
<body>
<input type="text" onfocus="fldx=this" />
<input type="text" onfocus="fldx.focus();" />
</body>
</html>
10:41 pm on Dec 7, 2005 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:4988
votes: 12


I tried to get this working but with no luck. My initial thought was that you'd need to add an onkeypress event handler that would stopPropagation of the event if the keyCode was 9 (the tab key) and the input value was invalid. Then you'd need another handler for the onblur event to set the focus back to this one. But I couldn't get it working.

A possible alternative... instead of checking the value on the blur event, you could check it on the form submit event, as part of some validation. Then highlighting and setting the focus should be easier (I think).

Good luck

12:28 am on Dec 8, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 2, 2003
posts:3710
votes: 0


Try this

globalvar = thisfield;
setTimeout("globalvar.focus()",250);

Kaled.

9:28 am on Dec 8, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 29, 2004
posts:49
votes: 0


hey cool, i'll give that a try. thanks!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members