Solved

getElementById javascript set focus error in FireFox

Posted on 2008-06-20
7
7,340 Views
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"
                                          onblur="javascript:check_val('RS_damage_roof','0,1,2,3,4,5');">

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);
   thefield.focus();
}
0
Comment
Question by:Roland_F
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

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

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

or try this:

var thefield = fname;
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 125 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 );
0
 

Author Comment

by:Roland_F
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 41

Expert Comment

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

Author Comment

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

Author Closing Comment

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

Expert Comment

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

Good luck & have a great day
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now