Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attach event problem

    Howdy
    I'm trying to attach a function to the 'onlick' event of all spans within a page (not my real aim, just a simplyfied version) without touching the markup, but for some reason I'm not getting any response...
    Here's the script:
    Code:
    // dummy function
    function blah() {
    alert('It Worked!');
    }
    // 
    function makeClicky() {
     var words = document.getElementsByTagName("span");
     for (var i=0; i<words.length; i++) {
       var word = words[i];
         word.onlick = blah;
    }
    }
    
    window.onload = makeClicky;
    Here's the HTML:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Text Test</title>
    <link rel="stylesheet" type="text/css" href="text.css"/>
    <script src="text.js" type="text/javascript"></script>
    </head>
    <body>
    <div class="text">
    	<h2>Tortoise and the Hare</h2>
    	<p>The <span class="noun">Hare</span> <span class="verb">ran</span> past the <span class="adj">lazy</span> <span class="noun">Tortoise</span>.</p>
    </div>
    </body>
    </html>
    Adding onlick="blah();" to the spans works fine, but that's not an option.

    Man, I've really got to learn this JS stuff ... Any Aussies out there want to swap some JS training for some CSS training?
    Last edited by mindlessLemming; 12-13-2004 at 06:36 AM.

    I take no responsibility for the above nonsense.


    Left Justified

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You were missing a curly brace:
    Code:
    // dummy function for testing
    function blah() {
    alert('It Worked!');
    }
    // Main function
    function makeClicky() {
     var words = document.getElementsByTagName("span");
     for (var i=0; i<words.length; i++) {
       var word = words[i];
         word.onclick = blah;
    }
    }
    // init.
    window.onload = function() {
    	makeClicky();
    }
    Edit: I see you found it...
    Last edited by Brandoe85; 12-13-2004 at 06:36 AM.

  • #3
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, found it, and another syntax error... Unfortunately that's not the problem.
    Thanks for looking though

    I take no responsibility for the above nonsense.


    Left Justified

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    It seems to be doing what your telling it to, you click on one of the span words, and it alerts it worked. Or am I misunderstanding what your trying to do?

  • #5
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Brandoe85
    It seems to be doing what your telling it to, you click on one of the span words, and it alerts it worked.
    It works for you????! It doesn't work for me in IE6 or FF1.0

    I take no responsibility for the above nonsense.


    Left Justified

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yes it's working fine for me. IE6, and FF.

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Is the code you posted all thats in the text.js file?

  • #8
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep. I just copied and pasted the HTML back from this post and it started working! BAHAHAHAaa, I love being an idiot

    In my defense though, it's actually client side XML + XSLT that is creating a virtual version of that html file. I just recreated the output so I had something to post with the script.
    Thanks again for your help

    I take no responsibility for the above nonsense.


    Left Justified

  • #9
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You're welcome, It's usually always something little like that lol


  •  

    Posting Permissions

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