Delete, Create, Copy HTML id attributes with Javascript


I'm just wondering if it's possible to delete, create and copy an HTML element with specific ID attributes. For instance,

<tr id="row1"><td>field</td></tr>
<tr id="row2"><td>field 2</td></tr>
Is it possible to delete the first row with id attribute "row1" using Javascript? 
If it is then is it possible to copy and create the second row or the first row based on the id attribute?
Could you give me a quick sample of the Javascript if this is possible? I believe it is using Javascript or AJAX.

Open in new window

Victor KimuraSEO, Web DeveloperAsked:
Who is Participating?
niko86Connect With a Mentor Commented:
here are some quick samples based on the html provided by you:
<tr id="row1"><td>field</td></tr>
<tr id="row2"><td>field 2</td></tr>
<script type="text/javascript">
/*delete first row:*/
/*clone Element:*/
var row2El=document.getElementById("row2");
var clone=row2El.cloneNode(true);
/*get Element with id row2, clone Content into variable 'clone'*/
/*true or false refers to cloning the content*/
/*now do whatever you want with the cloned element*/
/*in this case we are appending it to the row2 Element*/

Open in new window

Victor KimuraSEO, Web DeveloperAuthor Commented:
Great that works, niko86. Now I just have to figure out how to prevent duplicate ids.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.