/    Sign up×
Community /Pin to ProfileBookmark

dynamically add html controls using javascript in php page

hi
I’m trying to add html controls dynamically.
and I successful in that using clone method.

But I lost my way …..
I confused how to retrieve the values from the controls which I had cloned.
Below is the simplified code i had done.

Please help me in retrieving the values. I’m new in using dom objects?

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>
<title></title>
<script language=”javascript” type=”text/javascript”>
function Add(id){
var table=document.getElementById(id);
var clone=table.getElementsByTagName(‘TBODY’)[0].cloneNode(true);
table.appendChild(clone);
counter++;
}
</script>
</head>

<body>
<form name=”form1″ method=”post” action=””>

<input type=”button” name=”” value=”Add” onclick=”Add(‘tst’);”/>
<input type=”button” name=”getValues” value=”getValues” onClick=”getValue()”>
<table id=”tst” >
<tbody>
<tr>
<td colspan=”7″ bgcolor=”#EEEEEE”><b>Residential Address Details</b></td>
</tr>
<tr>
<td colspan=”1″> Address Line 1 </td>
<td colspan=”6″>
<input type=”text” style=”width: 300″ class=”inputsubmit” name=”address_line_1″ id=”address_line_1″ onChange=”saveValues1(this.value)”>
</td>
</tr>
<tr>
<td colspan=”1″> Address Line 2 </td>
<td colspan=”6″>
<input type=”text” style=”width: 300″ class=”inputsubmit” name=”address_line_2″ id=”address_line_2″ onChange=”saveValues2(this.value)”>
</td>
</tr>
<tr>
<td colspan=”1″> Country of Residence </td>
<td colspan=”6″>
<input type=”text” class=”inputsubmit” name=”country_residence” style=”width:35&#37;” onChange=”javascript:this.value=this.value.toUpperCase();” id=”country_residence”/>
&nbsp;&nbsp;&nbsp;&nbsp; Residency Type&nbsp;
<select id=”residence_perm_temp” name=”residence_perm_temp” class=”inputsubmit2″>
<option selected value=””>Permanent</option>
<option value=”temporary”>Temporary</option>
</select>
</td>
</tr>
</tbody>
</table>
</form>
</body>

</html>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 10.2011 — After you clone the elements, you must change (probably using an indentation) the [B]name[/B] and the[B] id [/B]of the form's elements.

Here's an example:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
var clone;
function addRow(but){
var tbo=but.parentNode.parentNode.parentNode;
tbo.appendChild(clone);
var rows=tbo.getElementsByTagName('tr');
clone=rows[rows.length-1].cloneNode(true);
orderNames(rows)
}
function removeRow(but){
var thisRow=but.parentNode.parentNode;
var tbo=thisRow.parentNode;
tbo.removeChild(thisRow);
var rows=tbo.getElementsByTagName('tr');
orderNames(rows)
}
function orderNames(rows){
var i=0,r,j,inp,t,k;
while(r=rows[i++]){
inp=r.getElementsByTagName('input');j=0;k=1;
while(t=inp[j++]){
if(t.type=="text"){t.name='col_'+k+'_row_'+i;k++;}
}
}
}
onload=function(){
clone=document.getElementById('mytab').getElementsByTagName('tr')[0].cloneNode(true)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=""&gt;
&lt;table width="600" border="1" cellpadding="0" cellspacing="0" id="mytab"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type="text" name="col_1_row_1"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="col_2_row_1"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="col_3_row_1"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="button" value="ADD NEW ROW" onclick="addRow(this)"&gt;&lt;br&gt;&lt;br&gt;
&lt;input type="button" value="REMOVE THIS ROW" onclick="removeRow(this)"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MSharkawySep 29.2017 — Thank you so much...

I'm working on a web application with a Customer Proposal Module which determines the work order > delivery order > invoice and I've been stuck on adding multiple items..

You've really helped... Thank you..
×

Success!

Help @hisuryahere spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...