Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. dynamic button id , javascript

dynamic button id , javascript

Scheduled Pinned Locked Moved JavaScript
javascripthelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    williamroma
    wrote on last edited by
    #1

    hi i have a button , and when i click it i want to add a new input text and i want to add an ID for this new input text , how can i do it by javascript cos i want to press the button many times and so i have to have many input text , each one with a uniqe ID i worked like this

    var num=1;
    function on(){
    var div = docuemnt.getElementbyID('div1');
    div.innerHTML+="";
    }

    but doesn't work with me :( :( please any help :)

    A 1 Reply Last reply
    0
    • W williamroma

      hi i have a button , and when i click it i want to add a new input text and i want to add an ID for this new input text , how can i do it by javascript cos i want to press the button many times and so i have to have many input text , each one with a uniqe ID i worked like this

      var num=1;
      function on(){
      var div = docuemnt.getElementbyID('div1');
      div.innerHTML+="";
      }

      but doesn't work with me :( :( please any help :)

      A Offline
      A Offline
      avatar23
      wrote on last edited by
      #2

      HI,williamroma First, from looking at your code i noticed that you use the pre tag. In order for a script to be executed it must be enclosed in tags. Second, Javascript is case sensitive so the method getElementbyID will work only if it is written getElementById. Third, += works welll for numbers, but for string you need something like string1=string1+string2. Forth, all kinds of input work when they are placed inside a form tag, and for buttons you need to specify what happens when you click them using something like onclick. heres a refined version of your code which worked for me: <script language="Javascript"> var num=1; function on(){ var div = document.getElementById('frm1'); div.innerHTML=div.innerHTML + "<input type='text' id='in'+num />"; }

      avatar23

      Q 1 Reply Last reply
      0
      • A avatar23

        HI,williamroma First, from looking at your code i noticed that you use the pre tag. In order for a script to be executed it must be enclosed in tags. Second, Javascript is case sensitive so the method getElementbyID will work only if it is written getElementById. Third, += works welll for numbers, but for string you need something like string1=string1+string2. Forth, all kinds of input work when they are placed inside a form tag, and for buttons you need to specify what happens when you click them using something like onclick. heres a refined version of your code which worked for me: <script language="Javascript"> var num=1; function on(){ var div = document.getElementById('frm1'); div.innerHTML=div.innerHTML + "<input type='text' id='in'+num />"; }

        avatar23

        Q Offline
        Q Offline
        quan_q
        wrote on last edited by
        #3

        which browser do you use?That code does not work for me,my English is so poor,I hope you can understand what i say, ;)

        A 1 Reply Last reply
        0
        • Q quan_q

          which browser do you use?That code does not work for me,my English is so poor,I hope you can understand what i say, ;)

          A Offline
          A Offline
          avatar23
          wrote on last edited by
          #4

          hi, I have tested the code in chrome, opera and IE 8,9. seems to be working fine, except that in ie you need to enable activeX objects to make it work properly. Iv have done a little tinkering with it and here is the new version. just copy it in ahtml file and give it a go! var num=1; function on(){ var div = document.getElementById('frm1'); div.innerHTML=div.innerHTML + "<input type='text' id='in'+num />"; num=num+1; }

          Best regards!

          My My A new one!!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups