By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

How can i identify a label?

P: 28
How can i identify a label on javascript?

I create labels dynamically and i add a onclick event for all of them.
But i need to identify which label has been clicked.

I tried to do this:

labelName.onclick = function(){functionName(nameOfLabel);}

but he donīt add the parameter for each label..the function always get the last parameter add.

sorry for the bad english,

thanks
Jan 23 '08 #1
Share this Question
Share on Google+
10 Replies


P: 28
what a i think i need is to pass dynamic parameters to a label event on javascript
Jan 23 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Show the rest of the code.
Jan 24 '08 #3

P: 28
// I create a variable number of labels based on a search on a XML file
// Each label receive the The ID LabelTest1, LabelTest2, LabelTest3 ...
// and the follow code is where i tried to define a function for the labels...changing just the parameter

Expand|Select|Wrap|Line Numbers
  1. for(i=0; i<productLength; i++)
  2. {
  3.     var labelID = "LabelTest"+ i;
  4.     var labelName = document.getElementById(labelID);
  5.     labelName.onclick = function(){functionName(labelID );
  6. }
Jan 28 '08 #4

gits
Expert Mod 5K+
P: 5,333
for loops we need kind of 'explicit' closures :) try the following:

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < productLength; i++) {
  2.     var labelID = "LabelTest"+ i;
  3.     var labelName = document.getElementById(labelID);
  4.  
  5.     labelName.onclick = function(l_id) { 
  6.         return function() { functionName(l_id) };
  7.     }(labelID);
  8. }
kind regards
Jan 28 '08 #5

P: 18
// I create a variable number of labels based on a search on a XML file
// Each label receive the The ID LabelTest1, LabelTest2, LabelTest3 ...
// and the follow code is where i tried to define a function for the labels...changing just the parameter

Expand|Select|Wrap|Line Numbers
  1. for(i=0; i<productLength; i++)
  2. {
  3.     var labelID = "LabelTest"+ i;
  4.     var labelName = document.getElementById(labelID);
  5.     labelName.onclick = function(){functionName(labelID );
  6. }

Try labelName.setAttribute('onclick',"function();");
Jan 28 '08 #6

gits
Expert Mod 5K+
P: 5,333
Try labelName.setAttribute('onclick',"function();");
this will not work in IE ...

kind regards
Jan 28 '08 #7

P: 28
for loops we need kind of 'explicit' closures :) try the following:

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < productLength; i++) {
  2.     var labelID = "LabelTest"+ i;
  3.     var labelName = document.getElementById(labelID);
  4.  
  5.     labelName.onclick = function(l_id) { 
  6.         return function() { functionName(l_id) };
  7.     }(labelID);
  8. }
kind regards
thanks a lot...it works

thanks for the help
Jan 28 '08 #8

gits
Expert Mod 5K+
P: 5,333
no problem ... glad i could help you ... post back to the forum anytime you have more questions :)

kind regards
Jan 28 '08 #9

Plater
Expert 5K+
P: 7,872
Try labelName.setAttribute('onclick',"function();");
that's for .NET, not javascript?
Jan 28 '08 #10

gits
Expert Mod 5K+
P: 5,333
that's for .NET, not javascript?
hi ... not necessaryly ... the setAttribute() dom-method is a standard method to set an attribute and its value ... and it should work with the onclick ... since it is an attribute too ... but IE (who is wondering about it?) will not work that way with the onclick attribute and only works when you set the node's onclick javascript-property

kind regards
Jan 28 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.