...

View Full Version : Assigning onclick event to labels



Kagura-San
07-04-2007, 05:00 PM
Hi,

I'm having trouble assigning an onclick event to labels. An example of what I'm trying to do is here:

http://lol.si/label-onclick/behaviour.html
http://lol.si/label-onclick/js.js

First I iterate through every label element that has "gas" class. Than I try to assign it an onclick="displayProperties(var)" function, but for some reason I can't.

When I check the "Generated HTML source", there is no onclick function there... Firebug doesn't report any errors whatsoever.

This is the problematic code (I guess):


function findGas() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName('label');
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute('class') === 'gas') {
anchor.onclick = function() {
displayProperties(anchor.getAttribute('for'));
}
}
}
}

Bill Posters
07-04-2007, 06:01 PM
...
if (anchor.getAttribute('class') === 'gas') {
anchor.onclick = function() {
displayProperties(anchor.getAttribute('for'));
...

Try...

...
if (anchor.className === 'gas') {
anchor.onclick = function() {
displayProperties(this.htmlFor);
...

Kor
07-04-2007, 06:05 PM
What about:

displayProperties(this.getAttribute('for'));}

Bill Posters
07-04-2007, 06:10 PM
What about:

displayProperties(this.getAttribute('for'));}

No-go in IE7 (or below, I presume)?

Kor
07-04-2007, 06:17 PM
Just a first thought idea

Kagura-San
07-04-2007, 06:52 PM
Try...

displayProperties(this.htmlFor);

It returns 'undefined' if I alert() it ... :/


displayProperties(this.getAttribute('for'));}
This throws an error - this.getAttribute is not a function

Bill Posters
07-04-2007, 07:01 PM
It returns 'undefined' if I alert() it ... :/
I checked it in FF/Win, IE7 and Op9/Win and it worked perfectly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum