473,657 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with deleting the last row in a dynamic Javascript table

Ok, I have this dynamically created table in my one of my php forms
that shows the names of the people the user has entered into a text
field. When they hit add a row displays, showing the name they entered.
Also, an image that allows them to delete shows beside the name upon
creation. The delete removes the name from the table and pushes the
other names to the top. Simple stuff, however, if I delete the last
name displayed(the bottom of the table), then the next, then the next,
IN ORDER, I have no problems. If I pick a name that is in the middle or
top of the table, everything below does not allow me to delete

For instance

Enter name (Text field)
BUTTON

X 1
X 2
X 3

If I clicked the x on 3,then 2, then 1...no problems, if i click 2,
then 1, then i cannot delete 3
if I click 1, then i cannot delete 2, or 3

I hope that explains it, Here is my function that does it it feeds off
another function to delete the rows, and I have tried
tbl.DeleteRows( RowIndex) and I have tried lastRow(My tbl length var) by
its self and -1

Please help

function addGranteeNameT oDisplay()
{
var oCell;
var oRow;
var tbl = document.getEle mentById( 'granteeNameDis playTable' );
var lastRow = tbl.rows.length ;
var iteration = lastRow-1;

//Update the 'hidden' value grantorNames so that when the user
//checks out we will have it
var name = document.getEle mentById('grant eeLastName').va lue;
if (name != "" || document.getEle mentById('grant eeFirstName').v alue
!= '')
{
//if there is a first name, add it with a comma
if( document.getEle mentById('grant eeFirstName').v alue != '' )
name = name + ', ' +
document.getEle mentById('grant eeFirstName').v alue;

//add name to hidden value on form
document.getEle mentById('grant eeNames').value = name+'|';
oRow = tbl.insertRow(l astRow);
oCell = oRow.insertCell (0);
var imgAnchor = document.create Element( 'a' );
imgAnchor.oncli ck = function(){
var names = new Array();

names = document.getEle mentById('grant eeNames').value .split(',');
names.splice( index, 1 );
document.getEle mentById('grant eeNames').value =
names.join('|') ;
//tbl.deleteRow(l astRow);

removeRowFromTa ble( 'granteeNameDis playTable',last Row);
document.getEle mentById('grant eeLastName').va lue = '';
document.getEle mentById('grant eeFirstName').v alue = '';
return;
//addGranteeNameT oDisplay();
}
imgAnchor.onmou seover = function(){ return escape( 'Click here to
delete' ) };
imgAnchor.setAt tribute( 'href', 'javascript:voi d(0)' );

var imageNode = document.create Element( 'img' );
imageNode.setAt tribute( 'border', 0 );
imageNode.setAt tribute( 'src', pathToImages+'d elete.gif' );
imageNode.setAt tribute( 'width', 15 );
imageNode.setAt tribute( 'height', 15 );
imgAnchor.appen dChild( imageNode );
oCell.appendChi ld( imgAnchor );
var oCell2 = oRow.insertCell (1);
var textNode = document.create TextNode(lastRo w);

if( name )
textNode.data = name;

oCell2.appendCh ild( textNode );

document.getEle mentById('grant eeLastName').va lue = '';
document.getEle mentById('grant eeFirstName').v alue = '';

}

}
function removeRowFromTa ble( tableID, index )
{
var tbl = document.getEle mentById(tableI D);
var lastRow = tbl.rows.length ;
// add one because index is zero based and number of rows is 1 based
tbl.deleteRow( index );
return;
}

Feb 17 '06 #1
5 2471
anyone?

Feb 19 '06 #2
VK
var lastRow = tbl.rows.length ;
// add one because index is zero based and number of rows is 1 based
tbl.deleteRow( index );

Maybe I'm missing something:- but all arrays and collections in
JavaScript are zero-based. So a table with 3 rows has rows.length = 3
and the last row has index [2]:
rows[0]
rows[1]
rows[2]

Feb 19 '06 #3
JonH wrote:
Ok, I have this dynamically created table in my one of my php forms
that shows the names of the people the user has entered into a text
field. When they hit add a row displays, showing the name they entered.
Also, an image that allows them to delete shows beside the name upon
creation. The delete removes the name from the table and pushes the
other names to the top. Simple stuff, however, if I delete the last
name displayed(the bottom of the table), then the next, then the next,
IN ORDER, I have no problems. If I pick a name that is in the middle or
top of the table, everything below does not allow me to delete

For instance

Enter name (Text field)
BUTTON

X 1
X 2
X 3

If I clicked the x on 3,then 2, then 1...no problems, if i click 2,
then 1, then i cannot delete 3
if I click 1, then i cannot delete 2, or 3

I hope that explains it, Here is my function that does it it feeds off
another function to delete the rows, and I have tried
tbl.DeleteRows( RowIndex) and I have tried lastRow(My tbl length var) by
its self and -1

Please help

function addGranteeNameT oDisplay()
{
Use 2 or 4 spaces for indenting, don't use tabs. It will help to stop
auto-wrapping.

var oCell;
var oRow;
var tbl = document.getEle mentById( 'granteeNameDis playTable' );
var lastRow = tbl.rows.length ;
var iteration = lastRow-1;

//Update the 'hidden' value grantorNames so that when the user
//checks out we will have it
var name = document.getEle mentById('grant eeLastName').va lue;
You make several calls to getElementById for granteeLastName ,
granteeFirstNam e & granteeNames. Why not do it once for each and store
references (e.g. as you've done with tbl)?
if (name != "" || document.getEle mentById('grant eeFirstName').v alue
!= '')
{
//if there is a first name, add it with a comma
if( document.getEle mentById('grant eeFirstName').v alue != '' )
name = name + ', ' +
document.getEle mentById('grant eeFirstName').v alue;

//add name to hidden value on form
document.getEle mentById('grant eeNames').value = name+'|';
oRow = tbl.insertRow(l astRow);
oCell = oRow.insertCell (0);
var imgAnchor = document.create Element( 'a' );
imgAnchor.oncli ck = function(){
var names = new Array();
Declaring names as an empty array here does nothing of value, just
modify the line below:


names = document.getEle mentById('grant eeNames').value .split(',');
var names = document.getEle mentById('grant eeNames').value .split(',');

names.splice( index, 1 );
document.getEle mentById('grant eeNames').value =
names.join('|') ;
//tbl.deleteRow(l astRow);

removeRowFromTa ble( 'granteeNameDis playTable',last Row);
You already have a reference to granteeNameDisp layTable called 'tbl',
why not pass that?

You have initialised 'lastRow' with a value of tbl.rows.length , then
pass that as the index to delete. So the delete function only ever
works if you delete the last row.

document.getEle mentById('grant eeLastName').va lue = '';
document.getEle mentById('grant eeFirstName').v alue = '';
return;
//addGranteeNameT oDisplay();
}
imgAnchor.onmou seover = function(){ return escape( 'Click here to
delete' ) }; [...]
}
function removeRowFromTa ble( tableID, index )
{
var tbl = document.getEle mentById(tableI D);
var lastRow = tbl.rows.length ;
Here you get lastRow, but don't use it. You use the value passed from
the main function, which is tbl.rows.length .
// add one because index is zero based and number of rows is 1 based
tbl.deleteRow( index );
return;
}

Here is a simplified example of what you are trying to do, maybe it will
help:
<script type="text/javascript">

function addName(tableID , inputID)
{
var tbl = document.getEle mentById(tableI D);
var txt = document.getEle mentById(inputI D).value;
var oR = tbl.insertRow(-1);
var oC = oR.insertCell(0 );
var oA = document.create Element('a');
oA.href = '#';
oA.onclick = function () {delRow(this);r eturn false;};
oA.appendChild( document.create TextNode('delet e'));
oC.appendChild( oA);
oC = oR.insertCell(1 );
oC.appendChild( document.create TextNode(txt));
}

function delRow(el)
{
while (el.parentNode && 'tr' != el.nodeName.toL owerCase()){
el = el.parentNode;
}
if ('tr' == el.nodeName.toL owerCase()){
el.parentNode.r emoveChild(el);
}
}

</script>
<table id="namesTable" >
<tr>
<td><input type="text" id="inputName"> </td>
<td><input type="button" value="Add name"
onclick="addNam e('namesTable', 'inputName');"> </td>
</tr>
</table>


--
Rob
Feb 20 '06 #4
JRS: In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
dated Sun, 19 Feb 2006 15:34:22 remote, seen in
news:comp.lang. javascript, VK <sc**********@y ahoo.com> posted :
var lastRow = tbl.rows.length ;
...


If you find that, when you start a News reply, Google does not provide
the previous article in quoted form, note what Keith Thompson wrote in
comp.lang.c, message ID <ln************ @nuthaus.mib.or g> :-
If you want to post a followup via groups.google.c om, don't use
the "Reply" link at the bottom of the article. Click on "show
options" at the top of the article, then click on the "Reply" at
the bottom of the article headers.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Feb 20 '06 #5
After a few modifications with the original code I was able to get it
to work. Rob, thank you again for your help, as well as everyone who
replied. I inherited this code with my new job and I had to make
modifications for some users. That, coupled with my lack of overall
knowledge of Javascript made this difficult for me to decipher. Thank
you all again, very much

Feb 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
22205
by: AdamG | last post by:
I am trying hard for days now to add and delete rows to a table. I could really use some help... Each row contains two buttons (images) - 'add row' and 'delete row'. When the user clicks add row within a specific cell/row, the index of that row should be passed to a function that creates a new row using that index (the new row should be added directly below the row where the user clicked. The new row should contain all of the cells and...
6
3309
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It is possible to Add and Delete rows - Some cells have special attributes (readonly and events) Here's a snippet of the code:
24
9877
by: Robi | last post by:
I have the following problem: I populate a page with a specific amount of <div id="MyTest"> containers inside another <div> container. for (i=0; i < MyString.length; i++) document.write('<div id="MyTest" style="position:absolute;top:0px;left:0;height:12;width:12;text-align:center">'+MyString+'</div>'); Now if MyString contains 6 characters, I end up with 6 <div id="MyTest"> containers. With IE I am able to access these containers as...
5
2595
by: comshiva | last post by:
Hi all, I have converted my existing ASP.NET project from 1.1 to 2.0 and i have found that everything works fine except the linkbutton control in my datagrid which throws an javascript error when clicked. I thought the control might be the problem, so i deleted the old control and binded the new linkbutton control but am still getting the same error. Am using visual studio 2005. Source code inside my grid:...
7
9774
by: Jon Maz | last post by:
Hi, I have a MySql problem I hope someone can help me with. I'm trying to run an update on a linking table, the update is running into a Primary Key constraint violation, and in my workaround I've got stuck trying to write a DELETE statement. Here's the table I'm working on: CREATE TABLE `articles_categories` (
1
3749
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem, and I have spent three days trying to debug but to no avail. Everything is OK when there are only two dependent list boxes, but when adding the third child list box, a problem appears: if I populate the third box only with the value: new...
2
3006
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my local system that validations work.plz tell that whats the problem in that. Here is my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html...
3
4205
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row value (text value) here is the code if possible please help me Thanks beforehand <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.