[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16755
  • Last Modified:

document.getElementById(thisObject) returns null

I have function 'calculate(thisObject)' which is triggered by the OnChange event on a number of textfields:

            ...
            <td>
              <input type="textfield" name="field1" value=0.00
                  onFocus="this.select()"
                  onChange="calculate(this)">
            </td>
            <td>
              <input type="textfield" name="field2" value=0.00
                  onFocus="this.select()"
                  onChange="calculate(this)">
            </td>
            ...

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" );
      thisObject.value=fzero.toFixed(2);

      // PROBLEM LINE
      setTimeout( document.getElementById(thisObject).focus(), 10 );

  } else {
     document.jobform.TW.value=w.toFixed(2);
  }

}

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.

Thanks.
0
siliconbrit
Asked:
siliconbrit
1 Solution
 
robotman757Commented:
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:

<td>
              <input type="textfield" name="field1" id="field1" value=0.00
               onFocus="this.select()"
               onChange="calculate(this)">
            </td>
            <td>
              <input type="textfield" name="field2" id="field2" value=0.00
               onFocus="this.select()"
               onChange="calculate(this)">
            </td>

the getElementById function needs an id to work, at least that has been my experience.
0
 
justinbilligCommented:
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( );
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now