473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

cloneNode and appendChild problem in adding table row

This is a shortened version of my problem. Below I am cloning the first
data row and appending it to create a new row. If you make selections/add
values and then press Add Row, the text box value is carried to the new
row's text element also (the select doesn't retain it's selected value).
How can I create a new row with the form elements in the original no value
state? I tried cloning the Node on page load, but then it only allows me to
add one row. Is there a way to clone the row and retain it to add as many
rows as I like. I know I can clear all the values of the row after I create
it, but that seems like a long way around especially since I have many
fields in my actual code.

Any pointers appreciated,

John

<form name="myForm" method="post">
<input type="button" value="Add Row" onclick="addRow()">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
<tbody id="datatable">
<tr>
<td>
<select name="mySelect">
<option>Select</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</td>
<td>
<input type="text" name="myTextfield">
</td>
</tr>
</tbody>
</table>
</form>
<script language="JavaScript">
function addRow(){
var myTbody=document.getElementById('datatable');
newRow=myTbody.firstChild.cloneNode(true);
myTbody.appendChild(newRow);
}
</script>
Jul 20 '05 #1
3 14504


johkar wrote:
This is a shortened version of my problem. Below I am cloning the first
data row and appending it to create a new row. If you make selections/add
values and then press Add Row, the text box value is carried to the new
row's text element also (the select doesn't retain it's selected value).


I have made a test case for that recently for a bugzilla entry for Mozilla
http://bugzilla.mozilla.org/attachme...42&action=view
If you try that test case with different browsers (Mozilla, IE, Opera)
then you will find that if you want to ensure across browsers that the
state of a HTML form control (e.g. the value property for text controls,
the checked property for radio or checkbox controls, the selectedIndex
for select) is cloned you need to do that in your script explicitly.
Opera 7 doesn't clone any of the above named properties, and Mozilla
some of them, and IE/Win some other.
As cloneNode is part of the W3C DOM Core module there is unfortunately
nothing to be demanded from the W3C DOM specification what should happen
with properties of HTML form controls.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
"johkar" <no********@link.net> writes:
Below I am cloning the first data row and appending it to create a
new row. If you make selections/add values and then press Add Row,
the text box value is carried to the new row's text element also
(the select doesn't retain it's selected value). How can I create a
new row with the form elements in the original no value state?
The cloneNode method copies all the attributes, including the value
attribute, so there is no way to copy the row but not the values
using cloneNode.
I tried cloning the Node on page load, but then it only allows me to
add one row. Is there a way to clone the row and retain it to add
as many rows as I like.


Same way. Just remember to insert a *new* clone each time. So, onload
you make a clone of the empty row. Each time you want to insert a new
row, you make a new clone of the first clone, and insert it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Thank you both. I thought I had tried to "clone my clone" before, but I
went back and tried it the next day and it worked like a charm. Late day I
guess.

John

"johkar" <no********@link.net> wrote in message
news:Pl***************@newsread2.news.pas.earthlin k.net...
This is a shortened version of my problem. Below I am cloning the first
data row and appending it to create a new row. If you make selections/add
values and then press Add Row, the text box value is carried to the new
row's text element also (the select doesn't retain it's selected value).
How can I create a new row with the form elements in the original no value
state? I tried cloning the Node on page load, but then it only allows me to add one row. Is there a way to clone the row and retain it to add as many
rows as I like. I know I can clear all the values of the row after I create it, but that seems like a long way around especially since I have many
fields in my actual code.

Any pointers appreciated,

John

<form name="myForm" method="post">
<input type="button" value="Add Row" onclick="addRow()">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Col 1</td>
<td>Col 2</td>
</tr>
<tbody id="datatable">
<tr>
<td>
<select name="mySelect">
<option>Select</option>
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
</td>
<td>
<input type="text" name="myTextfield">
</td>
</tr>
</tbody>
</table>
</form>
<script language="JavaScript">
function addRow(){
var myTbody=document.getElementById('datatable');
newRow=myTbody.firstChild.cloneNode(true);
myTbody.appendChild(newRow);
}
</script>

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
7
by: garthb | last post by:
Hello, In Mozilla (Firefox 1.0.7) I can cloneNode a file input element that has a selected file value and appendChild it to another form without a problem. IE 6 loses the selected file value....
1
by: jaktharkhan | last post by:
Hi, I really really need help in trying to figure out how can I do a CloneNode on an Iframe where the cloned IFRAME clones with all its contents?. Basically what I am doing is dynamically building...
1
by: bs9999 | last post by:
Can someone please tell me why when i use <object>.clondeNode that when I appendChild it to my document, the new node doesnt properly work? I'm making a clone of a <scriptand <linknode from an...
4
by: Olorin | last post by:
Hello everyone. I'm playing around with some AJAX-ish stuff and encountered some problem in the JS side of the universe. Maybe someone here can suggest an alternative that works. I have...
3
by: santosh.pjb | last post by:
I have a button on a page whose onclick funtion is posted below. I am basically trying to get all the spans in the page and list them in 2 columns. I get the list of spans using...
2
by: Alino | last post by:
hello, i need help with this javascript <script type="text/javascript"> function addfile() { var obj = document.getElementById("sprytextfield2").cloneNode(true);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.