SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some completly noobish help

    Well, i turned away a bit from php to learn javascript too. And i know, i will get myself a book, but still, i would like to learn first how to make something verry simple.
    Like when you have external JS, it would look for all the elements with certain ID and apply click event to them. My html code is like this:
    Code:
    <html>
    	<head>
    		<title> JS - Testing </title>
    		<script type='text/javascript' src='test.js'></script>
    	</head>
    	<body>
    		<div id='pov'>this is a link.</div>
    		
    		<p>On the occasion of this 100th anniversary, Vodafone McLaren Mercedes drivers Lewis Hamilton and Heikki Kovalainen 
    		will drive demonstration laps in Magny-Cours with an original 1908 Benz Grand Prix car.</p>
    	</body>
    </html>
    And here is my JS code:
    Code:
    	var link = document.getElementById('pov');
    	link.onclick = test();
    
    	function test()
    	{
    		alert('links');
    	}
    Now the problem is, whatever i do, even if i use object instead of this way, i end up with error (FF firebug):

    link has no properties
    link.onclick = test();

    the problem is, it seems, that document.getElementById('pov'); can't find element with that ID, even though you can clearly see it does exist, and always returns NULL value. Why so? What should i do to make it work?

    Sorry for noobish question; i do know a bit of JS, but i'm starting to learn from basics again, and this is something i don't understand? It's probably something verry simple.
    tnx.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change

    Code javascript:
    link.onclick = test();

    to

    Code javascript:
    link.onclick = test;


  3. #3
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did, it still says link has no properties.
    Tnx for trying to help though.
    Seems like problem is in "getElementById". Any ideas?

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok the error looks like its because your calling it directly in the javascript file rather then using an onload event handler.

    Basically, the div Pov doesn't exist at the point your code is running.

    use

    Code javascript:
    window.onload = function() {
    var link = document.getElementById('pov');
        link.onclick = test;
    }

    Once the page has loaded, it will run your code.


  5. #5
    SitePoint Zealot
    Join Date
    Nov 2007
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah ofcourse, tnx a lot!


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
  •