homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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

 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)) {
alert("Du skrev:\n" + thisField.value + "\n\nmen kun tall er tillatt.");

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

 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">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<script type="text/javascript">
<input type="text" onfocus="fldx=this" />
<input type="text" onfocus="fldx.focus();" />


 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


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

Try this

globalvar = thisfield;



 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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved