homepage Welcome to WebmasterWorld Guest from 54.198.224.121
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
focus() in firefox not working properly
SEOViking




msg:1480693
 8:25 pm on Dec 6, 2005 (gmt 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);" />

 

Rambo Tribble




msg:1480694
 1:56 pm on Dec 7, 2005 (gmt 0)

Firefox, on Linux anyway, has some issues with focus, though I hadn't encountered this before. See https://bugzilla.mozilla.org/show_bug.cgi?id=311956

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>

Fotiman




msg:1480695
 10:41 pm on Dec 7, 2005 (gmt 0)

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

kaled




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

Try this

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

Kaled.

SEOViking




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved