dynamic button id , javascript
-
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 :)
-
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 :)
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
-
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
-
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, ;)
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!!!