Assign different names for each dynamically created button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • likhin M
    New Member
    • Aug 2007
    • 5

    Assign different names for each dynamically created button

    Hi everyone,

    I am developing a webpage using only javascript.And I am dynamically creating buttons inside javascript as follows

    Code:
      document.write('<button name="" onclick="">button1</button>');

    For each button I am creating , I have to use different names.I have tried by using a javascript string variable to hold the name of the button, and using it
    in the name attribue of the button .But this is not working.How can I assign different names for each button I am dynamically creating using javascript?
    pls help me.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    the variable-trick you already used should do what you want ... please show what you have tried with this ...

    kind regards

    Comment

    • likhin M
      New Member
      • Aug 2007
      • 5

      #3
      Originally posted by gits
      hi ...

      the variable-trick you already used should do what you want ... please show what you have tried with this ...

      kind regards

      But this is not working .Here is the code...
      Code:
      <html>
      <head>
      <script>
      c1=new TreeCtrl("lm1","1");
      c1.create();
      
      
      function TreeCtrl(label,index)
      {this.label=label;
      this.f=0;
      this.BtnStr;
      this.DivStr;
      this.index=index;
      this.n1=0;
      this.n2=0;
      this.n3=0;
      this.add=add;
      this.create=create;
      this.expand=expand;
      this.collapse=collapse;
      this.check=check;
      
      }
      
      
      
      
      function add(n1,n2,n3)
      {this.n1=n1;
      this.n2=n2;
      this.n3=n3;
      }
      
      function create()
      {
      
      this.BtnStr="button"+this.index;
      this.DivStr="div"+this.index;
      
      document.write('<button onclick="check();" name=c1.BtnStr>+</button> &nbsp');
      document.write(this.label);
      document.write("<br>");
      document.write("<div id=c1.DivStr style='display:none'>");
      document.write("&nbsp&nbsp&nbsp&nbsp Level1.1");
      document.write("<br>");
      document.write("&nbsp&nbsp&nbsp&nbsp Level1.2");           
      document.write("<br>");
      document.write("&nbsp&nbsp&nbsp&nbsp Level1.3");
      document.write("</div>");
      document.write("<br>");
      }
      
      function expand()
      {button1.value="-";
      var e=document.getElementById(this.DivStr);
      e.style.display="block";
      f=1;
      }
      
      function collapse()
      {button1.value="+";
      var e=document.getElementById(this.DivStr);
      e.style.display="none";
      f=0;
      
      }
      
      function check()
      {if(this.f==0)
      this.expand();
      
      else
      this.collapse();
      
      }
      
      
      
      
      
      
      
      
      </script>
      </head>
      
      </html>

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        the following will not work:

        [CODE=javascript]
        document.write( '<button onclick="check( );" name=c1.BtnStr> +</button> &nbsp');
        [/CODE]

        adapt it with the following:

        [CODE=javascript]
        document.write( '<button onclick="check( );" name="' + varname + '"/>&nbsp');
        [/CODE]

        kind regards

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          I've changed the thread title. Please use a good thread title. Thanks!

          Comment

          • likhin M
            New Member
            • Aug 2007
            • 5

            #6
            [QUOTE=gits]hi ...

            the following will not work:

            [CODE=javascript]
            document.write( '<button onclick="check( );" name=c1.BtnStr> +</button> &nbsp');
            [/CODE]

            adapt it with the following:

            [CODE=javascript]
            document.write( '<button onclick="check( );" name="' + varname + '"/>&nbsp');
            [/CODE]

            kind regards

            Thank u for helping me.
            But this syntax is also not working.Here the name assigned to button is simply
            "' + varname + '" instead of the value contained in the variable.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              Originally posted by likhin M
              Thank u for helping me.
              But this syntax is also not working.Here the name assigned to button is simply
              "' + varname + '" instead of the value contained in the variable.
              you have to replace the variable varname with your desired variable of course ... it IS working ;) try to do the following and have a look wheter this is working or not:

              [CODE=javascript]var varname = 'test';
              document.write( '<button onclick="check( );" name="' + varname + '"/>&nbsp');
              [/CODE]

              kind regards

              Comment

              Working...