SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript error console - element has no properties?

    here is the code i used
    Code:
    document.write(timer);
    var start = {
    	clickHandler:function(){
    		alert("This is a simple click handler .");
    	}
    };
    var elmnt=document.getElementById("test");
    elmnt.onclick=start.clickHandler;
    and the html code
    Code:
    <a href="#" id="test">Test Me</a>
    I think every bite of code is right , but when i try to execute , i get the error
    elmnt has no properties .
    I am confused everything appears to be syntax correct.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the DOM tree is created when your script executes?

    If you put your code as-is in the <head>, it will run as the parser gets to it – i.e., before it gets to your anchor element with id="test".

    If that's the problem, either move the script to the bottom of the document source, or add an event listener to run it when the document has loaded.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that done the trick .
    we can’t assign an event handler to our element until the element
    has loaded
    maybe i have to read better .
    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •