Solved

getElementById javascript set focus error in FireFox

Posted on 2008-06-20
7
7,350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

756 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