How can i identify a label?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guilherme21922
    New Member
    • Jan 2008
    • 28

    How can i identify a label?

    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.oncli ck = function(){func tionName(nameOf Label);}

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

    sorry for the bad english,

    thanks
  • guilherme21922
    New Member
    • Jan 2008
    • 28

    #2
    what a i think i need is to pass dynamic parameters to a label event on javascript

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Show the rest of the code.

      Comment

      • guilherme21922
        New Member
        • Jan 2008
        • 28

        #4
        // 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...changi ng just the parameter

        [CODE=javascript]for(i=0; i<productLength ; i++)
        {
        var labelID = "LabelTest" + i;
        var labelName = document.getEle mentById(labelI D);
        labelName.oncli ck = function(){func tionName(labelI D );
        }[/CODE]
        Last edited by gits; Jan 28 '08, 02:30 PM. Reason: added code tags

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          for loops we need kind of 'explicit' closures :) try the following:

          [CODE=javascript]
          for (i = 0; i < productLength; i++) {
          var labelID = "LabelTest" + i;
          var labelName = document.getEle mentById(labelI D);

          labelName.oncli ck = function(l_id) {
          return function() { functionName(l_ id) };
          }(labelID);
          }[/CODE]
          kind regards

          Comment

          • Nibbus
            New Member
            • Nov 2007
            • 18

            #6
            Originally posted by guilherme21922
            // 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...changi ng just the parameter

            [CODE=javascript]for(i=0; i<productLength ; i++)
            {
            var labelID = "LabelTest" + i;
            var labelName = document.getEle mentById(labelI D);
            labelName.oncli ck = function(){func tionName(labelI D );
            }[/CODE]

            Try labelName.setAt tribute('onclic k',"function(); ");

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              Originally posted by Nibbus
              Try labelName.setAt tribute('onclic k',"function(); ");
              this will not work in IE ...

              kind regards

              Comment

              • guilherme21922
                New Member
                • Jan 2008
                • 28

                #8
                Originally posted by gits
                for loops we need kind of 'explicit' closures :) try the following:

                [CODE=javascript]
                for (i = 0; i < productLength; i++) {
                var labelID = "LabelTest" + i;
                var labelName = document.getEle mentById(labelI D);

                labelName.oncli ck = function(l_id) {
                return function() { functionName(l_ id) };
                }(labelID);
                }[/CODE]
                kind regards
                thanks a lot...it works

                thanks for the help

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  no problem ... glad i could help you ... post back to the forum anytime you have more questions :)

                  kind regards

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by Nibbus
                    Try labelName.setAt tribute('onclic k',"function(); ");
                    that's for .NET, not javascript?

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      Originally posted by Plater
                      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

                      Comment

                      Working...