/    Sign up×
Community /Pin to ProfileBookmark

How to change the row id according after deleting dynamically

Hi,

In my page i have to add rows dynamically and remove rows dynamically. I have one checkbox and three text boxes for each row which is added dynamically.

Adding and deleting rows works fine. I have problem after deleting. This is due to unchange of id value for checkbox and textboxes.

I assigned the id value in array and incremented while adding and decremented while removing. But the the id and values of the textboxes is not exact. How to fix this?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienSep 08.2006 — Can you post the HTML and JavaScript involved here? I have a couple rough ideas, but nothing that would really help.
Copy linkTweet thisAlerts:
@vijirubaauthorSep 09.2006 — Here is my code:

If i add two rows dynamically and delete the first row, after deleting the checkbox and textboxes id should be changed accordingly.

[CODE]<script language = "Javascript" type="text/javascript">
function AddRows()
{
var tableId = document.getElementById('filtertable');
var count = document.getElementById('rowCnt').value;
var noOfRows = tableId.rows.length;
var ptrRef = noOfRows;

var tableRow = tableId.insertRow(noOfRows);
count++;

// Checkbox
var chkBoxCell = tableRow.insertCell(0);
var chkBoxColElem = document.createElement('input');
chkBoxColElem.type = 'checkbox';
chkBoxColElem.name = 'chkbox[]';
chkBoxColElem.id = 'chkbox' + ptrRef;
chkBoxCell.appendChild(chkBoxColElem);

//Emp Name text box
var secondCell = tableRow.insertCell(2);
var secondColElem = document.createElement('input');
secondColElem.type = 'text';
secondColElem.name = 'empName[]';
secondColElem.id = 'empName' + ptrRef;
secondCell.appendChild(secondColElem);

//Emp designation text box
var thirdCell = tableRow.insertCell(3);
var thirdColElem = document.createElement('input');
thirdColElem.type = 'text';
thirdColElem.name = 'empDesgn[]';
thirdColElem.id = 'empDesgn' + ptrRef;
thirdCell.appendChild(thirdColElem);
}
function RemoveRows()
{
var tableId = document.getElementById('filtertable');
var count = document.getElementById('rowCnt').value;
var noOfRows = tableId.rows.length;
if(count > 1)
{
for(i=1;i<count;i++)
{
itemCheck="chkbox"+i;
if(document.getElementById(itemCheck)!=undefined)
{

if(document.getElementById(itemCheck).checked==true) {
tableId.deleteRow(i);
count--;
}
}
}
} else {
tableId.deleteRow(noOfRows - 1);
count--;
}
document.getElementById('rowCnt').value = count;
}
</script>

<form name="dynamicRows" method="post" action="<?php echo $PHP_SELF; ?>" Autocomplete="off">
<table id="dynamicTable">
<tr>
<td><input type="checkbox" name="chkbox[]" id="chkbox1"></td>
<td><input type="textbox" name="empName[]" id="empName1"></td>
<td><input type="textbox" name="empDesgn[]" id="empDesgn1"></td>
</tr>
<tr>
<td><input type="button" value="Addrow()"></td>
<td><input type="button" value="RemoveRow()"></td>
</tr>
</table>
</form>[/CODE]


After deleting deleting first row my second row's checkbox id should be changed to "chkbox1" and thired row's checkbox id should be changed to "chkbox2".
×

Success!

Help @vijiruba spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...