Object Required?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbguy7040
    New Member
    • May 2010
    • 3

    Object Required?

    So I just started learning HTML and Javascript about a week ago, and while I'm writing this code, I get an error saying "Object Required". I don't understand why the object is required, and how to fix it, so could you please help?

    Also, I probably have other errors as well, so if you could suggest any other corrections to my code, that would be great!

    The error states that the object is required on line 2, character 1.

    The following code is all the Javascript code I have in the head section of my HTML document so far...

    Code:
    <script type="text/javascript">
    var command=document.getElementById('tc').innerHTML;
    function checkCommand() {
      if (command=="add todo") {
        addToDo();
      }
    }
    function startTime()  {
      var today=new Date();
      var h=today.getHours();
      var m=today.getMinutes();
      var s=today.getSeconds();
      m=checkTime(m);
      s=checkTime(s);
      document.getElementById('time').innerHTML=h+":"+m+":"+s;
      t=setTimeout('startTime()',500);
    }
    function checkTime(i) {
      if (i<10) {
        i="0" + i;
      }
      return i;
    }
    function myDate() {
      var d=new Date();
      var year=d.getFullYear();
      var month=d.getMonth()+1;
      var date=d.getDate();
      document.getElementById('date').innerHTML=month+"/"+date+"/"+year;
    }
    function addToDo() {
      var input=prompt("Type what you need To-Do...","Make it short please!");
      var tdv=document.getElementById('todo').innerHTML;
      if (tdv.length>0) {
        tdv=tdv+"<br />"+input;
      }
      else {
        tdv=input;
      }
    }
    </script>
    Thanks!!!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you usually get an "Object required" error, when you execute code before the appropriate HTML has been built (say, if you have your script in the <head> section, line 2 will throw that error, because nothing of the <body> has been rendered yet). the way to go would be to delay any code execution until the page has completed loading, either via window.onload/window.addEvent Listener()/any such cross-browser event solution.

    besides, that is one reason why you should avoid global variables.

    Comment

    Working...