tags:

views:

1045

answers:

2

Well, it works, it just doesn't produce anything worthwhile:

elems = document.getElementById("itemsTable").getElementsByTagName("TR") 
for j = 0 to ubound(elems) - 1      
   ' stuff 
next

Well, that won't work, apparently elems is an object, not an array like you'd get in that fancy javascript. I'm stuck with vbscript though.

So what do I do to iterate all the rows in a table in vbscript?

Edit: Yes, it's vbscript and it sucks. I don't have a choice here, so don't say "Use jQuery!!".

A: 

elems isn't an array in JavaScript either, it is a NodeList, it just happens to share some properties with a JavaScript Array object.

I don't know VB, but I assume you could do:

for j = 0 to elems.length - 1      
   ' stuff 
next
David Dorward
Note that I said "apparently elems is an object". The object type doesn't have a length.
jcollum
Wow. Sounds like VB has a broken DOM implementation.
David Dorward
+4  A: 

As you have correctly stated getElementsByTagName does not return an array, hence UBound() will not work on it. Treat it as a collection.

For-Eaching through it should work:

 Set NodeList = document.getElementById("itemsTable").getElementsByTagName("TR") 
 For Each Elem In NodeList
  ' stuff 
  MsgBox Elem.innerHTML
 Next
Yannick M.
Excellent, thanks. I never did figure out why you have to Set things sometimes and others you don't.
jcollum
If I can I'll put a bounty on this, just so you get more rep for the answer. Answering vbscript questions on SO is a thankless job.
jcollum
Haha, that's cool. Btw check this page for your question regarding set: http://www.empinstitute.org/caspdoc/html/vbscript_set_statement.htm
Yannick M.