473,776 Members | 1,574 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 5328
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
4887
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
8225
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
16948
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
2891
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
13565
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
1612
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
1851
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
9628
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
9464
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
10120
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...
1
10061
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,...
1
7471
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
5367
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
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2860
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.