...

View Full Version : Attach event problem



mindlessLemming
12-13-2004, 05:25 AM
Howdy :D
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:


// 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:


<!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 :o... Any Aussies out there want to swap some JS training for some CSS training? :rolleyes:

Brandoe85
12-13-2004, 05:33 AM
You were missing a curly brace:


// 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();
}

I see you found it...

mindlessLemming
12-13-2004, 05:37 AM
Yeah, found it, and another syntax error... Unfortunately that's not the problem.
Thanks for looking though :)

Brandoe85
12-13-2004, 05:46 AM
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?

mindlessLemming
12-13-2004, 06:15 AM
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
:confused:

Brandoe85
12-13-2004, 06:19 AM
Yes it's working fine for me. IE6, and FF.

Brandoe85
12-13-2004, 06:22 AM
Is the code you posted all thats in the text.js file?

mindlessLemming
12-13-2004, 06:32 AM
Yep. I just copied and pasted the HTML back from this post and it started working! BAHAHAHAaa, I love being an idiot :o

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 :)

Brandoe85
12-13-2004, 06:33 AM
You're welcome, It's usually always something little like that lol :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum