Solved

Delete, Create, Copy HTML id attributes with Javascript

Posted on 2009-04-03
2
501 Views
Last Modified: 2012-05-06
HI,

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


<table>

<tr id="row1"><td>field</td></tr>

<tr id="row2"><td>field 2</td></tr>

</table>
 

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

0
Comment
Question by:Victor Kimura
2 Comments
 
LVL 5

Accepted Solution

by:
niko86 earned 500 total points
ID: 24065928
here are some quick samples based on the html provided by you:
<table>

<tr id="row1"><td>field</td></tr>

<tr id="row2"><td>field 2</td></tr>

</table>

<script type="text/javascript">

/*delete first row:*/

document.getElementById("row1").parentNode.removeChild(row1);

/*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*/

row2El.appendChild(clone);

/*in this case we are appending it to the row2 Element*/

</script>

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 31566513
Great that works, niko86. Now I just have to figure out how to prevent duplicate ids.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I   ajax html table  rows? 20 60
Javascript Hashmap output 8 43
Javascript and Jquery not firing 9 37
Objects on Same Line 2 15
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now