473,388 Members | 1,188 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,388 software developers and data experts.

cloning objects and CSS id of cloned element

abs
Hi all.
I use such code to clone blocks:

element = document.getElementById(IdOfElement)
element = element.cloneNode(true)
new_element = document.getElementById(IdOfElement).appendChild(e lement)

Anybody knows how to set id for the new element ? I mean the id which I coud
use in CSS stylesheet for setting css properties of new element.

Best regards,
ABS
Jul 23 '05 #1
3 6939
abs wrote:
Hi all.
I use such code to clone blocks:

element = document.getElementById(IdOfElement)
element = element.cloneNode(true)
new_element = document.getElementById(IdOfElement).appendChild(e lement)

Anybody knows how to set id for the new element ? I mean the id which I coud
use in CSS stylesheet for setting css properties of new element.

Best regards,
ABS

This will work

element = document.getElementById(IdOfElement)
element = element.cloneNode(true)
element.id = 'newID'; //<- ID Assignment
new_element = document.getElementById(IdOfElement).appendChild(e lement)

HTH

Regards,
N Demos

=== Example Code ===

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">
<HTML>
<HEAD>
<TITLE>CloneTest</TITLE>
<SCRIPT language="javascript" type="text/javascript">
<!--
function cloneAndInsert(IdOfElement)
{
element = document.getElementById(IdOfElement);
element = element.cloneNode(true);
element.id = 'new' + element.id;

new_element = document.getElementById(IdOfElement).appendChild(e lement);
return true;
}
// -->
</SCRIPT>
<STYLE type="text/css">
BODY {
background-color: #000000;
color: #FFFFFF;
}
A {
display: inline;
border-style: solid;
border-color: #0000AA;
border-width: 4px;
background-color: #0000FF;
padding: 5px 10px 5px 10px;
vertical-align: middle;
}
A:link {
color: #FF0000;
}
A:visited {
color: #800000;
}
A:hover {
border-style: outset;
color: #00FF00;
}
A:active {
border-style: inset;
color: #FF00FF;
}

#div1 {
border-style: outset;
border-color: #00FF00;
border-width: 2px;
background-color: #FF0000;
color: #000000;
}
#newdiv1 {
border-style: inset;
border-color: #AAAAFF;
border-width: 2px;
background-color: #0000FF;
color: #FFFFFF;
}
</STYLE>
</HEAD>

<BODY>
<DIV id="div1">
<P>A Paragraph</P>
<P>Another Paragraph</P>
</DIV>
<BR>
<A href='javascript:' onClick="cloneAndInsert('div1');">Click Here</A>
</BODY>
</HTML>
--
Change "seven" to a digit to email me.
Jul 23 '05 #2
abs
N. Demos wrote:
This will work [...]


It works ! Thank you for quick response.

Regards.
Jul 23 '05 #3
abs wrote:
I use such code to clone blocks:

element = document.getElementById(IdOfElement)
element = element.cloneNode(true)
new_element = document.getElementById(IdOfElement).appendChild(e lement)


Wouldn't

var element = document.getElementById(id),
newElement = element.cloneNode(true);

element.appendChild(newElement);

be a more sensible pattern? Anyway, as already indicated, placing

newElement.id = 'new-id';

before the appendChild call allows one to assign an id to the new element.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

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

Similar topics

1
by: Stuart | last post by:
How to clone objects which contain other objects and collections. 1. In top level class (Class1), write a clone method as follows: public object Clone() { Class1...
7
by: sonic | last post by:
Hello, I am cloning a table row which contains images that have behaviors attached to them as well as onclick events. The problem is that the cloned row seems to be executing the...
0
by: ryanr | last post by:
I've run into a problem with a recursive algorithm. Basically, it grabs a tree, and creates premutations based off it. For example: Suppose you have you sell shoes in two colors (Black,...
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
1
by: illegal.prime | last post by:
I would like to have an array of objects (whose class I define) and then just invoke either: MyClass clonedArray = (MyClass) myArray.Clone(); OR Array.Copy(myArray, clonedArray, myArray.Length);...
1
by: colmkav | last post by:
Hi, what is the main reason why cloning is done to tables in Access VBA code? regards Colm
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
9
gits
by: gits | last post by:
This short article introduces a method that may be used to create a 'deep-copy' of an javascript object. You might ask: 'Wherefore do we need this?' ... Answer: 'Only variable-values of the basic...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.