Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2006, 05:34 PM   PM User | #1
bevan
New Coder

 
Join Date: Jan 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
bevan is an unknown quantity at this point
please help me with dynamically adding textfields

I have this script that adds a new row textfields of text fields when the user clicks the button...

but I want the function to add 5 new fields of varying sizes instead of just 2.

can anyone help?

cheers

bevan



the code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="Javascript" type="text/javascript">
var Cnt=2;

function addField(area,field,limit) {
if(!document.getElementById) return;
var field_area=document.getElementById(area);
var li=document.createElement('LI');
field_area.appendChild(li);
var tb=document.createElement('INPUT');
tb.name='friend_A'+Cnt;
li.appendChild(tb);
tb=tb.cloneNode(false);
tb.name='friend_B'+Cnt;
li.appendChild(tb);
Cnt++;
}

</script>


</head>

<body>
<form name="frm" method="POST">
<br />
<ol id="friends_area"><li >
<input name="friend_1" type="text" id="friend_A1" size="4" />
<input name="friend_12" type="text" id="friend_B1" size="20" />
<input name="friend_123" type="text" id="friend_C1" size="10">
<input name="friend_1234" type="text" id="friend_D1" size="10">
<input name="friend_12345" type="text" id="friend_E1" size="7">
</li>
</ol>
<input type="button" value="Add Another row" onClick="addField('friends_area','friend_',10);" />

<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<br />
<br />
</form>

</body>
</html>
bevan is offline   Reply With Quote
Old 01-26-2006, 05:55 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="Javascript" type="text/javascript">
var Cnt=2;

function addField(area,field,limit) {
 if (Cnt==limit){ return; }
 if(!document.getElementById) return;
 var field_area=document.getElementById(area);
 var li=field_area.getElementsByTagName('LI')[0].cloneNode(true);
 field_area.appendChild(li);
 var ips=li.getElementsByTagName('INPUT');
 for (var zxc0=0;zxc0<ips.length;zxc0++){
  ips[zxc0].value='';
  ips[zxc0].name='friend_'+Cnt+zxc0;
  ips[zxc0].id='friend_A'+Cnt+zxc0;
  ips[zxc0].value=ips[zxc0].id; // just testing
 }
 Cnt++;
}

</script>


</head>

<body>
<form name="frm" method="POST">
<br />
<ol id="friends_area">
<li >
<input name="friend_11" type="text" id="friend_A11" size="4" />
<input name="friend_12" type="text" id="friend_A12" size="20" />
<input name="friend_13" type="text" id="friend_A13" size="10">
<input name="friend_14" type="text" id="friend_A14" size="10">
<input name="friend_15" type="text" id="friend_A15" size="7">
</li>
</ol>
<input type="button" value="Add Another row" onClick="addField('friends_area','friend_',10);" />

<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<br />
<br />
</form>

</body>
</html>
vwphillips is online now   Reply With Quote
Old 01-26-2006, 06:00 PM   PM User | #3
bevan
New Coder

 
Join Date: Jan 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
bevan is an unknown quantity at this point
amazing!! youre the best vwphillps!

best wishes

bevan
bevan is offline   Reply With Quote
Old 01-26-2006, 06:08 PM   PM User | #4
bevan
New Coder

 
Join Date: Jan 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
bevan is an unknown quantity at this point
ha ha, sorry

can u make it go further than 9 rows?

(I am trying to teach myself this stuff but its tough starting off, you know,)
bevan is offline   Reply With Quote
Old 01-26-2006, 06:45 PM   PM User | #5
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="Javascript" type="text/javascript">
var Cnt=2;

function addField(area,field,limit) {
 if (Cnt>limit){ return; }
 if(!document.getElementById) return;
 var field_area=document.getElementById(area);
 var li=field_area.getElementsByTagName('LI')[0].cloneNode(true);
 field_area.appendChild(li);
 var ips=li.getElementsByTagName('INPUT');
 for (var zxc0=0;zxc0<ips.length;zxc0++){
  ips[zxc0].value='';
  ips[zxc0].name='friend_'+Cnt+zxc0;
  ips[zxc0].id='friend_A'+Cnt+zxc0;
 }
 Cnt++;
}

</script>


</head>

<body>
Its all according how you interparate last parameter of addField( but just assign digits to meet requirement<br>
<form name="frm" method="POST">
<br />
<ol id="friends_area">
<li >
<input name="friend_11" type="text" id="friend_A11" size="4" />
<input name="friend_12" type="text" id="friend_A12" size="20" />
<input name="friend_13" type="text" id="friend_A13" size="10">
<input name="friend_14" type="text" id="friend_A14" size="10">
<input name="friend_15" type="text" id="friend_A15" size="7">
</li>
</ol>
<input type="button" value="Add Another row" onClick="addField('friends_area','friend_',44);" />
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<br />
<br />
</form>

</body>
</html>
vwphillips is online now   Reply With Quote
Old 01-26-2006, 07:13 PM   PM User | #6
bevan
New Coder

 
Join Date: Jan 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
bevan is an unknown quantity at this point
thanx vic got it,

have you seen my other post about adding the values and displaying them?

last one I promise.
bevan is offline   Reply With Quote
Old 01-26-2006, 07:35 PM   PM User | #7
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
yes

no problem, ask away
vwphillips is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:47 PM.


Advertisement
Log in to turn off these ads.