getElementById javascript set focus error in FireFox

Posted on 2008-06-20
Medium Priority
Last Modified: 2013-12-07
The following properly sets the focus to the input field being tested in IE, Opera, and Safari (Windows). It does not work in Firefox; instead the focus goes to wherever you sent it in order to leave the field.

In the form:  
<input type="text" size="3" name="RS_damage_roof" id="RS_damage_roof" value="0" class="num"

The javascript:
// Check value entered in a field
function check_val(fname,okay) {
      var thefield = document.getElementById(fname);
      var val = thefield.value;
      var okval = okay.split(',');
      for (var i=0;i<okval.length;i++) {
            if (val === okval[i] ) return true;
   alert("Please enter one of these values: " + okay);
Question by:Roland_F
  • 3
  • 3
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21831311
Try this:

 var thefield = document.getElementById('RS_damage_roof');

or try this:

var thefield = fname;
LVL 41

Accepted Solution

HonorGod earned 375 total points
ID: 21831395
In the past, I have gotten around this by having a function
to setFocusOn( id ), and using a slight delay to invoke it..

  // Name: focusOn( id )
  // Role: Provide a way to delay the field.focus() request
  // Note: The Gecko engine (Mozilla, Firefox, et.al.) may throw an
  //       uncatchable excption "Permission denied".  This has
  //       supposedly been fixed in Firefox 1.5.1
  function focusOn( id ) {
    var field = document.getElementById( id );
    if ( field ) {
      field.focus();    // May throw "Permission denied" (uncatchable)
    } else {
      alert( 'unknown section id "' + id + '"' );


 setTimeout( 'focusOn("myField")', 10 );

Author Comment

ID: 21832856
I had to go read about setTimeout to find out why I needed to define another function, then I had to figure out how to use my field id, which for me is in a variable because I'm dealing with a lot of fields -- both of these were very reasonable things to leave for me to figure out. My code is now working in FireFox. Thanks.
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

LVL 41

Expert Comment

ID: 21833568
So, is there anything else that is needed, or is this question answered?

Author Comment

ID: 21835222
I thought "Accept as solution" meant it was answered. Yes, it is.  I accepted your answer as the solution.  I thought.

Author Closing Comment

ID: 31469156
See my earlier comment. The solution was complete enough for me to figure out the rest.
LVL 41

Expert Comment

ID: 21835691
Glad to be able to help.  Thanks for the grade & points.

Good luck & have a great day

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When I'm searching for answers on Experts Exchange, I often use Google because it's built into my browser.  To search only on Experts Exchange, I use the "site:" search operator, which can be cumbersome to type out each time I want to run a search.…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
The viewer will learn how to dynamically set the form action using jQuery.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question