Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cloning labels

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pavan Keely
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code on how you are cloning and how you are accessing cloned labels.
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried accessing addcounty[0], addcounty[1] etc and was able to do it
 
Pavan Keely
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by reset I mean that the new text box that is created is empty.
 
Pavan Keely
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic