how to change id by adding new row..

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jun 2009
Posts: 119
Reputation: sam023 is an unknown quantity at this point 
Solved Threads: 2
sam023 sam023 is offline Offline
Junior Poster

how to change id by adding new row..

 
0
  #1
Aug 10th, 2009
i am making a table with dynamic increament of rows on click of button..!!

i have used an autocomplete textbox in a row...!!! which is based on textbox id..!!
.
.
as i know that id should be unique.. so how can i change ID then..?
  1. function add(oRow)
  2. {
  3. var selObj = oRow.getElementsByTagName('select')[0];
  4. if(selObj[0].selected){ // Check for empty ledger entry
  5. alert("Please select ledger");
  6. return false;
  7. }
  8. oRow.parentNode.replaceChild(oRow.cloneNode(true),oRow.parentNode.insertRow(oRow.rowIndex+1));
  9. // alert(oRow.rowIndex+1);
  10. var inpR = oRow.getElementsByTagName('input');
  11. var inpN = oRow.nextSibling.getElementsByTagName('input');
  12. var selR = oRow.getElementsByTagName('select')[0];
  13. //alert(selR);
  14. selR.disabled=true;
  15. var selN = oRow.nextSibling.getElementsByTagName('select')[0];
  16. selN.selectedIndex=0;
  17. for(i=0;i<inpR.length;i++)
  18. {
  19. if(inpR[i].disabled){inpR[i].disabled=false;/**/};
  20. if(inpR[i].type=='text'){inpR[i].disabled=true;inpN[i].value='';inpN[i].disabled=false};
  21. if(inpR[i].value=='Add'){inpR[i].value='Edit';inpN[i].disabled=true};
  22. } sumus();knockOut(selN);
  23. }
Last edited by sam023; Aug 10th, 2009 at 4:02 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 119
Reputation: sam023 is an unknown quantity at this point 
Solved Threads: 2
sam023 sam023 is offline Offline
Junior Poster

Re: how to change id by adding new row..

 
0
  #2
Aug 10th, 2009
i hope people understood here what exactly i want to say..!! :O
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 126
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: how to change id by adding new row..

 
0
  #3
Aug 10th, 2009
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:
  1. var n = oRow.getAttribute('counter');
  2. n = (n===null) ? 0 : n+1;
  3. oRow.setAttribute('counter', n);
Now clone the row, then:
  1. var suffix = '_' + n;
  2. 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
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to change id by adding new row..

 
0
  #4
Aug 11th, 2009
can you tell where should i make changes in my code..?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to change id by adding new row..

 
0
  #5
Aug 11th, 2009
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..!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 83
Reputation: nish123 is an unknown quantity at this point 
Solved Threads: 0
nish123's Avatar
nish123 nish123 is offline Offline
Junior Poster in Training

Re: how to change id by adding new row..

 
0
  #6
Aug 11th, 2009
  1. function add(oRow)
  2. {
  3. var selObj = oRow.getElementsByTagName('select')[0];
  4. if(selObj[0].selected){ // Check for empty ledger entry
  5. alert("Please select ledger");
  6. return false;
  7. }
  8. oRow.parentNode.replaceChild(oRow.cloneNode(true),oRow
  9. .parentNode.insertRow(oRow.rowIndex+1));
  10. var inpR = oRow.getElementsByTagName('input');
  11. var inpN = oRow.nextSibling.getElementsByTagName('input');
  12. var selR = oRow.getElementsByTagName('select')[0];
  13. //alert(selR);
  14. selR.disabled=true;
  15. var selN = oRow.nextSibling.getElementsByTagName('select')[0];
  16. selN.selectedIndex=0;
  17. for(i=0;i<inpR.length;i++)
  18. {
  19. if(inpR[i].disabled){inpR[i].disabled=false;/**/};
  20. if(inpR[i].type=='text'){inpR[i].disabled=true;inpN[i].value='';inpN[i].disabled=false};
  21. if(inpR[i].value=='Add'){inpR[i].value='Edit';inpN[i].disabled=true};
  22. } sumus();knockOut(selN);
  23. }

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..!!
Last edited by nish123; Aug 11th, 2009 at 6:54 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 63
Reputation: polo_coins is an unknown quantity at this point 
Solved Threads: 0
polo_coins polo_coins is offline Offline
Junior Poster in Training

Re: how to change id by adding new row..

 
0
  #7
Aug 11th, 2009
I don"t think change ID is a good Idea because in a future you may use they "id" in another cases
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 126
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: how to change id by adding new row..

 
0
  #8
Aug 11th, 2009
Originally Posted by nish123 View Post
can you tell where should i make changes in my code..?
Intriguing I'm sure but this is Sam023's topic.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 119
Reputation: sam023 is an unknown quantity at this point 
Solved Threads: 2
sam023 sam023 is offline Offline
Junior Poster

Re: how to change id by adding new row..

 
0
  #9
Aug 11th, 2009
Originally Posted by Airshow View Post
Intriguing I'm sure but this is Sam023's topic.

Airshow
Yes it is my topic but i can see that nish123 facing the same problem..!! and he also using same code as i do..!!!

so kindly help me out..!! where should i make changes..? in the code to create different id...!!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 119
Reputation: sam023 is an unknown quantity at this point 
Solved Threads: 2
sam023 sam023 is offline Offline
Junior Poster

Re: how to change id by adding new row..

 
0
  #10
Aug 11th, 2009
Originally Posted by polo_coins View Post
I don"t think change ID is a good Idea because in a future you may use they "id" in another cases
well i have no other option except changing ID..!!

as my auto complete function is based on id and ID must be unique..!!
.
.
as the user append the row... the clonenode function create exactly the same row as above including ID..!!! and that thing will create problem...!!!
.
.
can anyone suggest an auto complete function where ID is not required..?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC