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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assigning onclick event to labels

    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):
    Code:
    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'));
    			}
    		}
    	} 
    }
    Last edited by Kagura-San; 07-04-2007 at 04:04 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Code:
    ...
    		if (anchor.getAttribute('class') === 'gas') {
    			anchor.onclick = function() {
    				displayProperties(anchor.getAttribute('for'));
    ...
    Try...
    Code:
    ...
    		if (anchor.className === 'gas') {
    			anchor.onclick = function() {
    				displayProperties(this.htmlFor);
    ...

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    What about:

    displayProperties(this.getAttribute('for'));}
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by Kor View Post
    What about:

    displayProperties(this.getAttribute('for'));}
    No-go in IE7 (or below, I presume)?

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Just a first thought idea
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Bill Posters View Post
    Try...
    Code:
    displayProperties(this.htmlFor);
    It returns 'undefined' if I alert() it ... :/

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

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by Kagura-San View Post
    It returns 'undefined' if I alert() it ... :/
    I checked it in FF/Win, IE7 and Op9/Win and it worked perfectly.


  •  

    Posting Permissions

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