Sam,
If you never use document.getElementById on an element (or otherwise need to read its id) then it doesn't need one.
However, if an elememt does need an id, then as you say, it should be unique. One way to ensure uniqueness in a cloned element is to add a suffix, '_n' (where n is an integer). To ensure that "n" is unique, maintain an invisible counter in each of your table rows and increment it each time the row is duplicated.
Before duplicating the row:
var n = oRow.getAttribute('counter');
n = (n===null) ? 0 : n+1;
oRow.setAttribute('counter', n);
Now clone the row, then:
var suffix = '_' + n;
newRow.setAttribute('counter', 0);//Set to 0 in case the cloned row is itelf later cloned.
Now append suffix to all elements in the new row that need it.
New ids will be unique provided that each original id:is unique
is not of a value that could possibly occur due to the renumbering of another.
Airshow
Airshow
WiFi Lounge Lizard
2,678 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
can you tell where should i make changes in my code..?
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
actually i dont want to use ID...!! major reason is that i m also using a delete button which will delete the row..!!
.
.
this mean that again there can be same Id which will create problem..!! :(
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
function add(oRow)
{
var selObj = oRow.getElementsByTagName('select')[0];
if(selObj[0].selected){ // Check for empty ledger entry
alert("Please select ledger");
return false;
}
oRow.parentNode.replaceChild(oRow.cloneNode(true),oRow
.parentNode.insertRow(oRow.rowIndex+1));
var inpR = oRow.getElementsByTagName('input');
var inpN = oRow.nextSibling.getElementsByTagName('input');
var selR = oRow.getElementsByTagName('select')[0];
//alert(selR);
selR.disabled=true;
var selN = oRow.nextSibling.getElementsByTagName('select')[0];
selN.selectedIndex=0;
for(i=0;i<inpR.length;i++)
{
if(inpR[i].disabled){inpR[i].disabled=false;/**/};
if(inpR[i].type=='text'){inpR[i].disabled=true;inpN[i].value='';inpN[i].disabled=false};
if(inpR[i].value=='Add'){inpR[i].value='Edit';inpN[i].disabled=true};
} sumus();knockOut(selN);
}
i have a code for autocomplete in one of the textbox in a row.. but for that i need ID..!!
but because of clone i m unable to enter different id..!! where should i maker change in my javascript..!!
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
I don"t think change ID is a good Idea because in a future you may use they "id" in another cases
polo_coins
Junior Poster in Training
63 posts since Oct 2008
Reputation Points: 8
Solved Threads: 0
can you tell where should i make changes in my code..?
Intriguing I'm sure but this is Sam023's topic.Airshow
Airshow
WiFi Lounge Lizard
2,678 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
If all is working fine then you can use rowIndex as generating id.
That's fine the first time a row is cloned but on a second cloning of the same row, would it not generate an identical id to the first?
That's why I advocate maintaining a counter.Airshow
Airshow
WiFi Lounge Lizard
2,678 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
I dont think it is possible to change Id.. by using CloneNode...!!
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
Sam023,
Nish is right, CloneNode doesn't have an option for changing ID(s).
Have you tried the idea in my post #3 of this topic? It outlines a way (certainly not the only way) maintain a counter by which cloned elements can be force-renumbered for uniqueness.
Airshow
Airshow
WiFi Lounge Lizard
2,678 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372