473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

deleteRow isn't affecting table

The code below is part of a script that is imported by a webpage. I am
trying to modify a table on that webpage using this code. I can't
modify the actual webpage itself.
I would like to simply delete the last row from the table. After doing
the deleteRow method below, the outerHTML that is displayed in the
alert no longer contains the last row.
However, the actual row is still displayed on the webpage.

if(window.name == 'menuBar')
{
window.attachEvent("onload", navOnLoad);
}

function navOnLoad()
{
if (event.returnValue != false) {
var menuTable = document.getElementById("subnewAct");
var campResRow = document.getElementById("mnu_item_campaignact");

if(menuTable != null && campResRow != null)
{
linkIndex = campResRow.rowIndex;
document.all.subnewAct.deleteRow(linkIndex);
alert(document.all.subnewAct.outerHTML);
}
}
}

May 22 '06 #1
4 3729
ja***********@gmail.com wrote:
The code below is part of a script that is imported by a webpage. I am
trying to modify a table on that webpage using this code. I can't
modify the actual webpage itself.
I would like to simply delete the last row from the table. After doing
the deleteRow method below, the outerHTML that is displayed in the
alert no longer contains the last row.
However, the actual row is still displayed on the webpage.

if(window.name == 'menuBar')
{
window.attachEvent("onload", navOnLoad);
If you want this to work only in IE and are happy for other browsers to
throw errors, then the above is OK. To make it cross-browser, search
the archives for (W3C compliant) addEventListner and (IE's) attachEvent.

}

function navOnLoad()
{
if (event.returnValue != false) {
What is the purpose of this? It always returns false for me.

It is another IE-ism that doesn't seem to do anything useful. The
function will be called when the load event occurs (provided scripting
is enabled of course). The above makes subsequent script dependent on
support for IE's non-standard event model and will likely cause other
browsers to terminate the script.

var menuTable = document.getElementById("subnewAct");
var campResRow = document.getElementById("mnu_item_campaignact");

if(menuTable != null && campResRow != null)
{
linkIndex = campResRow.rowIndex;
Presumably "mnu_item_campaignact" is the id of a row in a table
somewhere and you would like to remove a row from another table that has
the same row index.

It might be handy to keep linkIndex local unless you have a good reason
for it to be global.

var linkIndex = campResRow.rowIndex;

document.all.subnewAct.deleteRow(linkIndex);
Here you require support for document.all, that is likely to fail in a
good majority of browsers, particularly if you are using HTML 4 strict.
You've already used getElementById to assign a reference to the table
with id subnewAct to menuTable, why not use it again?

It is safer to test that a row exists at linkIndex before trying to
deleted it:

if (menuTable.rows.length > linkIndex) {
menuTable.deleteRow(linkIndex);
}

If the requirement is just to delete the last row of menuTable, why not:

var t = menuTable.rows.length;
if (t){
menuTable.deleteRow(t - 1);
}
alert(document.all.subnewAct.outerHTML);
Presumably that is just for debug, it will work in IE but not too many
other browsers.
}
}
}


--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>
May 23 '06 #2
I've found the only reliable way to do this (for me) is to go

tableOb=document.getElementById('theTable');
rowOb=tableOb.rows[tableOb.length];
tableOb.removeChild(rowOb);

(or is that rowOb=tableOb.rows[tableOb.length-1]; ?)

had a similar issue with a drag and drop table rows system..
document.body.removeChild(rowOb); might be more reliable? these things
are quirky

May 23 '06 #3
gr******@gmail.com wrote:
I've found the only reliable way to do this (for me)
is to go

tableOb=document.getElementById('theTable');
rowOb=tableOb.rows[tableOb.length];
tableOb.removeChild(rowOb);
A TR element is never a child of a TABLE element in an HTML DOM (and
only sometimes in an XHTML DOM). In HTML they may only be children of
table section elements (TBODY, THEAD and TFOOT).
(or is that rowOb=tableOb.rows[tableOb.length-1]; ?)
If the preceding code is what you find "the only reliable way", why do
you ask the question? But yes, - tableOb.rows[tableOb.length] - will
refer to an undefined value in (beyond) the - rows - collection.
had a similar issue with a drag and drop table rows system..
document.body.removeChild(rowOb); might be more reliable?
A TR element will never be a child of a BODY element.
these things are quirky


Things will certainly seem quirky if you are operating entirely on the
basis of guess-work.

Richard.
May 23 '06 #4
gr******@gmail.com wrote:

Please quote what you are replying to, trim what you aren't.
I've found the only reliable way to do this (for me) is to go

tableOb=document.getElementById('theTable');
rowOb=tableOb.rows[tableOb.length];
tableOb.removeChild(rowOb);
That won't work because tableOb.rows[tableOb.length] will always be one
greater than the highest index, i.e. the index of the last row. The
indexes start at zero, for a table with 3 rows the highest index will be
2 (the rows have indexes: 0, 1, 2).

(or is that rowOb=tableOb.rows[tableOb.length-1]; ?)
As per my earlier reply: if menuTable is a reference to a table, to
remove the last row:

var t = menuTable.rows.length;
if (t){
menuTable.deleteRow(t - 1);
}

That uses the table's rows collection which contains all the rows in the
table. If you have a thead or tfoot, you may not wish to remove rows
from them so you'll need to use the tbody element and delete rows from
that. thead, tfoot and tboby elements all have a rows collection, they
implement the HTMLTableSection interface:

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-67417573>

If you have thead and tfoot elements, you may wish to use the table's
tbodys collection to get a reference to the last tbody, then remove the
last row from that. If you have multiple tbodys, you'll need a bit more
cleverness again.

had a similar issue with a drag and drop table rows system..
document.body.removeChild(rowOb); might be more reliable? these things
are quirky


Not really. removeChild will only work for the (direct) children of a
node - it is intended as a general way of removing child nodes from an
element. It works with documents that implement the core DOM
interfaces, such as HTML and XML documents:

<URL:http://www.w3.org/TR/DOM-Level-2-Core/>
deleteRow belongs to the HTML table and table section interfaces, which
are specific to HTML-based documents and are intended to provide quicker
ways of dealing with HTML 4 and XHTML 1.0 DOM objects:

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/>
A TR can't be a child of a body element, it must be a child of a table
section element - a thead, tfoot or tbody. If you don't explicitly code
a tbody element in your table, one will be inserted by the browser - the
tags are optional in the HTML source but a tbody element is mandatory in
the generated DOM. HTML 4 references:

HTML row groups (thead, tfood, tbody)
<URL:http://www.w3.org/TR/html4/struct/tables.html#edef-TBODY>

HTML table
<URL:http://www.w3.org/TR/html4/struct/tables.html#edef-TABLE>
--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>
May 23 '06 #5

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

Similar topics

2
by: Dom | last post by:
Hi, I am trying to get a dynamic table going. You click a button to add a row. In the newly created row a button to delete the row is placed (try the code example below). In Netscape, pressing...
2
by: John Geddes | last post by:
In both IE6 and Netscape 7: insertRow method of tbody is behaving as expected: - inserts a new row - increases tbody.rows.length BUT deleteRow is not doing the opposite. It DOES delete the...
5
by: Paul Reddin | last post by:
Hi, using ROWNUMBER() is affecting the plan of a view very badly... is there a way of writing the following view to ensure rownumber() is done as the last thing done? i.e after the calling...
4
by: wrytat | last post by:
I've a big problem that I don't know how to solve for quite long. In one of my aspx file, I have a form that run at server. Within this form, there's a table. The first row is made up of some text...
57
by: Chris Tomlinson | last post by:
Hi all, Hope someone is able to help. I notice when I design a basic HTML page, it is affected if a WinXP user has Large Fonts set in their Control Panel. However some pages, e.g....
2
by: awebguynow | last post by:
OK, so I can use deleteRow to do a subSelect of my table (dynamically generated). How can I show the table in full again ? The "back" button doesn't work b/c the dyn. gen. html was from a POST ...
2
by: User | last post by:
Hi Let's say I have a table that originally have 6 rows and contains 6 <Td> fields which stores data. If I were to use dom's deleteRow to remove the rows seamless *and* i wanna update the...
3
by: cbradio | last post by:
Hi, I am having trouble developing a form in a restricted environment. My sample code is found below my message (sorry I don't have a URL). Basically, without a doctype, the form displays properly...
6
by: ramyareddy | last post by:
Hi All, I am using MSSQL 2000 and 2005. i have a table memberSecurity with security numer as primary key. I have some data in the table already. Now i want to make secNo as auto increment and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.