This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes cloning labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "cloning labels" Watch "cloning labels" New topic
Author

cloning labels

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
hi,
I am trying to clone the following elements



In my javascript function i want to access individual labels name, how can i do it. In am using var valLabel = document.getElementById(labelID) to access label where labelID is argument passed to function called, but when i clone the above code to generate clone of the label and text box, I am not able to access the labels.

thnx in advace


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
Post your code on how you are cloning and how you are accessing cloned labels.


Pavan Keely<br /><a href="http://keelypavan.blogspot.com" target="_blank" rel="nofollow">http://keelypavan.blogspot.com</a>
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I have defined a table with id (say tableID). On clicking the button i call the following method for cloning



where tabID is the id of the table i am trying to clone
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
N,

I am not sure what you are trying to ask. Your first question was about accessing labels and your second question was about cloning table rows. So, I assume you have labels in table rows that you are trying to clone.

When you clone the nodes the same IDs will be given to the labels as the first row's labels. So, whenever you try to access the label with the document.getElementById(...), the first element will be returned matching that id. That means the first label in the first row. So, to access the second label and so on after cloning, you have to change the Ids of those elements (in this case, labels and input fields ). There is a non-standard way that works only in IE ( with document.all ) but I would not recommend that in this case.

Hope it's clear now. If this was not answer for your question, please elaborate.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Ya Pavan,
that is what i was trying to ask....I was just curious that if i clone the above elements (labels and text) than i can access the different addcounty by using addcounty[0], addcounty[1] etc... but i cant do the same for label.

Cant we access the label in similar manner?
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I tried accessing addcounty[0], addcounty[1] etc and was able to do it
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
N,

Here is the sample HTML I experimented with.




Let me remind you one more time, this is IE only solution.
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
To add one more point to my last reply, according to the standards, there should not be multiple elements with the same ID. This is quite against the rules.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
thank you pavan. I have one more question ... If i add



in the code, and the click add row button. A new row with same label and text is added. Now lets say that before i hit add button, i enter hello in the textbox. After entering hello in the text box i click the add row button, so that the new row with text box will have hello as it value. Is it possible to reset the value of the textbox while cloning.


thnx
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
by reset I mean that the new text box that is created is empty.
Pavan Keely
Ranch Hand

Joined: Jun 30, 2006
Posts: 62
N Chaurasia,

I don't think you can clone without the value. But anyways, I can think of one alternative for this. Before cloning the table row, set the value of the text field to empty then clone it and reset the value back. The pseudo code may look like this,



I think the prefered way, which will not deviate rules, is to use DOM methods or table methods to create rows.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cloning labels