document.getElementById(thisObject) returns null

Posted on 2004-11-17
Last Modified: 2012-05-05
I have function 'calculate(thisObject)' which is triggered by the OnChange event on a number of textfields:

              <input type="textfield" name="field1" value=0.00
              <input type="textfield" name="field2" value=0.00

var fzero=0.00;

function calculate(thisObject) {

   <snip - some code to calculate the values of 'w' and 'washed' >

   if (w > washed) {
      alert ("Sample weight can not be larger than Washed Weight" );

      setTimeout( document.getElementById(thisObject).focus(), 10 );

  } else {


The problem I have is that although the value gets set correctly in the line "thisObject.value=fzero.toFixed(2)", the line that attempts to set the focus fails with the following error:

   Error:  'document.getElementById(...)' is null or not an object.
   Code: 0

I've tried referencing the object directly:

   setTimeout( thisObject.focus(), 10 );

   Error:  invalid Argument
   Code: 0

...and attempted quoting in various ways ;-) ..but I cant get this code to set the focus correctly without either an error, or placing focus on the next element in the tab sequence.

I'm not a regular JS programmer and its not that important that I get this working, but I'm a bit of a perfectionist.  This is probably basic stuff for all you server-side hackers, and it bugs the hell out of me, so I would appreciate your comments.

Question by:siliconbrit
    LVL 3

    Accepted Solution

    The problem I believe is that you are missing the id's of the item. To call that function you need to give the object an ID like this:

                  <input type="textfield" name="field1" id="field1" value=0.00
                  <input type="textfield" name="field2" id="field2" value=0.00

    the getElementById function needs an id to work, at least that has been my experience.
    LVL 15

    Expert Comment

    the set timeout function takes two parameters, the command you want to execute as a string and the time

    setTimeout( "thisObject.focus()", 10 );

    but your setting the focus to that object in ten miliseconds. the user won't be able to notice the wait ( if that is what your going for )

    you could just do this

    thisObject.focus( );

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    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)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

    754 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

    16 Experts available now in Live!

    Get 1:1 Help Now