473,847 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

table.rows vs. table.getElemen tsByTagName (+ a regex question)

I am so frustrated. I've been working all weekend on what I thought would
be a simple script. I never find it easy to look at someone else's code,
but if someone can help with this I would be very grateful. The script is
....

<script type="text/javascript">
var parentwin = external.menuAr guments;
var doc = parentwin.docum ent;
var x = doc.getElements ByTagName("tabl e")
alert(x.length + " table things")
var re = /class='class_18 '>(\w+)<\/td>/

for (iv in x)
{
var tablerows = x[iv].rows
//alert(tablerows .length + " rows") // results in "'tablerows.len gth' is
// null or not an object ???????

//var tablerows2 = x[iv].getElementsByT agName("tr") // results in "Object
doesn't
// support this
property or method ??????

for (triv in tablerows)
{
alert("triv is \""+ triv + "\"; outerHTML:" +
tablerows[triv].outerHTML)

var marray = re.exec(tablero ws[triv].outerHTML)
//var marray = re.exec("<td align='center' class='class_18 '>Name</td>")
alert("marray is " + marray)
if (marray != null)
{ alert("match[0] " + marray[0])
alert("match[1] " + marray[1])
}
} // end of for (triv in tablerows)
} // end of for (iv in x)
</script>

And the HTML I am running it against is ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<table cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td align='center' class='class_18 '>Name</td>
<td align='center' class='prl5 class_10'| </td>
<td align='center' class='class_19 '>4328-5352</td>
</tr>
</table>
</html>

1) Why can't I get the length of tablerows after the var tablerows =
x[iv].rows statement? I'm only interested in a one-row table, so I could
save some processing if I could get the length.

2) Why doesn't x[iv].getElementsByT agName("tr") work?

3) Why do I go through the loop for (triv in tablerows) twice when there
is only one row in the table? AND when I go through the loop the first time
the value of triv is "length" and the value of the outerHTML is "undefined" ?

4) Why doesn't the regular expression work when fed the outerHTML but does
when fed the text contained in the HTML?

It was a frustrating weekend and I will appreciate any help anyone can
offer. Thanks, Bob
Jun 27 '08 #1
5 5332
eBob.com wrote:
I am so frustrated. I've been working all weekend on what I thought would
be a simple script. I never find it easy to look at someone else's code,
but if someone can help with this I would be very grateful. The script is
...

<script type="text/javascript">
var parentwin = external.menuAr guments;
var doc = parentwin.docum ent;
var x = doc.getElements ByTagName("tabl e")
alert(x.length + " table things")
var re = /class='class_18 '>(\w+)<\/td>/

for (iv in x)
x is a DOM node list, don't use for..in with that, use a for loop
for (var i = 0, l = x.length; i < l; i++)
{
var table = x[i];
}

for..in enumerates enumerable properties of an object and that is
usually not the right way to iterate over DOM collections.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2

"eBob.com" <eB******@total lybogus.comwrot e in message
news:gQ******** *************@f e03.news.easyne ws.com...
>I am so frustrated. I've been working all weekend on what I thought would
be a simple script. I never find it easy to look at someone else's code,
but if someone can help with this I would be very grateful. The script is
...

<script type="text/javascript">
var parentwin = external.menuAr guments;
var doc = parentwin.docum ent;
var x = doc.getElements ByTagName("tabl e")
alert(x.length + " table things")
var re = /class='class_18 '>(\w+)<\/td>/

for (iv in x)
{
var tablerows = x[iv].rows
//alert(tablerows .length + " rows") // results in "'tablerows.len gth'
is
// null or not an object ???????

//var tablerows2 = x[iv].getElementsByT agName("tr") // results in
"Object doesn't
// support this
property or method ??????

for (triv in tablerows)
{
alert("triv is \""+ triv + "\"; outerHTML:" +
tablerows[triv].outerHTML)

var marray = re.exec(tablero ws[triv].outerHTML)
//var marray = re.exec("<td align='center'
class='class_18 '>Name</td>")
alert("marray is " + marray)
if (marray != null)
{ alert("match[0] " + marray[0])
alert("match[1] " + marray[1])
}
} // end of for (triv in tablerows)
} // end of for (iv in x)
</script>

And the HTML I am running it against is ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<table cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td align='center' class='class_18 '>Name</td>
<td align='center' class='prl5 class_10'| </td>
<td align='center' class='class_19 '>4328-5352</td>
</tr>
</table>
</html>

1) Why can't I get the length of tablerows after the var tablerows =
x[iv].rows statement? I'm only interested in a one-row table, so I could
save some processing if I could get the length.

2) Why doesn't x[iv].getElementsByT agName("tr") work?

3) Why do I go through the loop for (triv in tablerows) twice when
there is only one row in the table? AND when I go through the loop the
first time the value of triv is "length" and the value of the outerHTML is
"undefined" ?

4) Why doesn't the regular expression work when fed the outerHTML but does
when fed the text contained in the HTML?

It was a frustrating weekend and I will appreciate any help anyone can
offer. Thanks, Bob
Sorry, I forgot to specify that I run the script under IE 6.0.
Jun 27 '08 #3
[trimmed attribution novel]

eBob.com wrote:
"eBob.com" <eB******@total lybogus.comwrot e:
>var parentwin = external.menuAr guments;
[...]
for (iv in x)
>{
var tablerows = x[iv].rows
//alert(tablerows .length + " rows") // results in "'tablerows.len gth'
is
//var tablerows2 = x[iv].getElementsByT agName("tr") // results in
"Object doesn't
// support this
property or method ??????
As you can see by now at last, pre-commenting is much better than
post-commenting. Your Question Mark key is borken, BTW.
> for (triv in tablerows)
{
alert("triv is \""+ triv + "\"; outerHTML:" +
tablerows[triv].outerHTML)
This can be written less error-prone and easier legible:

window.alert('t riv is "' + triv + '";'
+ ' outerHTML: ' + tablerows[triv].outerHTML);
> var marray = re.exec(tablero ws[triv].outerHTML)
Why not String.prototyp e.match() instead?
> //var marray = re.exec("<td align='center'
class='class_1 8'>Name</td>")
[...]

Sorry, I forgot to specify that I run the script under IE 6.0.
I suppose one can guess that from your using the `external' property.

However, Martin's correct explanation still stands. Iteration over the
enumerable properties of a NodeList object for which the reference is
returned by document.getEle mentsByTagName( "table") yields the `length'
property as well in IE 6. And a number value if implicitly converted
into a Number object does not have a getElementsByTa gName() method.

Same with the NodeList object reference that tableRows evaluates to.
tableRows["length"] yields a number, and the Number object it is being
converted to does not have an outerHTML property. The result of the
expression therefore is `undefined', and literally "undefined" when
implicitly converted to string by RegExp.prototyp e.exec(). So your
Regular Expression can't find a match.

Please trim your quotes as recommended e.g. in the FAQ Notes.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jun 27 '08 #4
Thank you very, VERY much Thomas and Martin! I would have struggled with
this problem for a long, long time without your help.

One loose end ... I have done some more research and learned about
Enumerators. I tried an Enumerator to step through the table rows and it
seems to work. Is there any practical difference between ...

for (var triv = 0, l = tablerows.lengt h; triv < l; triv++)

and ...

var enumTableRows = new Enumerator(tabl erows)
for (;!enumTableRow s.atEnd();enumT ableRows.moveNe xt())

?

Also, I discovered the problem with my regular expression. After an eyeball
tuneup I saw that document does quite a bit of massaging of the HTML. So my
regular expression, based on the original HTML, didn't have a chance of
working.

Thanks again for your very prompt and thorough help. Bob


Jun 27 '08 #5
eBob.com wrote:
One loose end ... I have done some more research and learned about
Enumerators. I tried an Enumerator to step through the table rows and it
seems to work. Is there any practical difference between ...

for (var triv = 0, l = tablerows.lengt h; triv < l; triv++)

and ...

var enumTableRows = new Enumerator(tabl erows)
for (;!enumTableRow s.atEnd();enumT ableRows.moveNe xt())

?
Enumerator is specific to Microsoft JScript, don't expect that to work
with other browsers or script engines.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #6

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

Similar topics

2
4894
by: Spanky | last post by:
Thanks for any help in advance! I have this order form where you add rows as you need them. The routine to add fields is working fine. I am trying to add the ability to delete rows if you check the checkbox in the cooresponding field and click remove item. I would appreciate any insite. BTW Am using IE browsers only. Thanks!
5
8229
by: Harry Gould | last post by:
To all, I'm a newbie here, so please bear with me. I develop web pages for a company intranet where Internet Explorer 6 is the standard. Now I must develop a public internet website that is browser-agnostic (i.e., works with Netscape, version 4x, 7x, etc). My question is this: I have about 10 table rows, each tagged with a class attribute (<tr class="billing" style="display:none">) that I wish to make visible or invisible in response...
3
16952
by: AR | last post by:
Hi, How can I hide table rows? ... tried with the following example: FireFox works... How to do the simillar in IE6? <html> <head> <script language="javascript"> function hide_row() { var v = document.getElementsByName("trBook"); v.style.display = 'none';
3
2893
by: Bijoy Naick | last post by:
I have something strange going on.. My .aspx page contains a file upload control, a "Import Data" button, a "newTransactions" <asp:table>, a"SaveTrans" button and an confMsg label. First the user selects a file, then clicks Import Data. The page now reads the PostedFile and displays each line as a new row in the newTransactions table. All of this works fine. For now, I've programmed SaveTrans_Click to display the number of rows in the...
1
301
by: JM | last post by:
Hi, I am not sure if this is the place to post a REGEX question if not, please indicate me where i can post it?. My question: Given a string like: "Boston. MA. Holidays" I need to define the regular expression (not other way) to find each dot, the previous word and the word after. That is i want to obtain:
7
13574
by: leiño | last post by:
Hi, i am adding table rows dynamically with javascript. This works fine. The table is inside a div with scrolls, initially with 6 rows: ..... <div style='overflow:auto; width:100%; height:100;'> <table> <tr><td>nothing</tr></td> <tr><td>nothing</tr></td>
4
312
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of URLs. (href=("|')http://.www\.thesite\.com/.{1,7}/)(?<filename>.) This generally works, but the problem is some of the image files have ..th.jpg at the end to indicate thumbnails. I want to exclude those. I just want the ones that don't have...
2
1618
by: akadeco | last post by:
Hi I need to write a script that will allow me to check the value of any link in a table cell. The desired result would be something like this: row.cell.innerHTML, allowing me to call the contents of the fourth cell in the first row. The following script may be of use as a starting point: <table id="table"> <tr> <td><a href="#">hello1</a></td> <td><a href="#">hello2</a></td>
1
1858
gskoli
by: gskoli | last post by:
Dear all Below i have given example of my HTML table structure Where two different <Td> are containing separate table both having same no. of rows but second table having some rows where textbox & dropdown is used which increasing the height of rows ..... So how i can manage that .... <td> <table> Having 14 row </table> </td> <td>
0
9892
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
10991
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10718
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10347
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...
0
9490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7061
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
5725
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
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3168
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.