views:

3105

answers:

5

Hello all, I'm looking to use a VBScript variable within a reference to a DOM element for a web-app I'm building. Here's a brief exerpt of the affected area of code:

dim num
num = CInt(document.myform.i.value)
dim x
x = 0
dim orders(num)
For x = 0 To num
    orders(x) = document.getElementById("order" & x).value
    objFile.writeLine(orders(x))
Next

This is my first venture into VBScript, and I've not been able to find any methods of performing this type of action online. As you can see in the above code, I'm trying to create an array (orders). This array can have any number of values, but that number will be specified in document.myform.i.value. So the For loop cycles through all text inputs with an ID of order+x (ie, order0, order1, order2, order3, order4, etc. up to num)

It seems to be a problem with my orders(x) line, I don't think it recognizes what I mean by getElementById("order" & x), and I'm not sure exactly how to do such a thing. Anyone have any suggestions? It would be much appreciated!

A: 

I can only assume that this is client side VBScript as document.getElementById() isn't accessible from the server.

try objFile.writeLine("order" & x), then check the source to make sure all the elements are in the document.

[As I can't put code in comments...] That is strange. It looks to me like everything should be working.

Only other thing I can think of is: change

orders(x) = document.getElementById("order" & x).value
objFile.writeLine(orders(x))

to

orders(x) = document.getElementById("order" & x)
objFile.writeLine(orders(x).value)
Dan Williams
You're right, it's just to help debug the problem. Making sure you're actually going through all the elements in the document.Have you tested what document.myform.i.value is returning?
Dan Williams
Also, I should mention that document.myform.i.value STARTS at 0. In order to get order0 - order4 above, i.value was changed to 5. So it depends on when the VBScript function runs.
That definitely makes sense, but it's still not working, I'm still getting Object Expected errors when the function runs.Are there any other ways to reference the DOM where a variable would be accepted?
A: 

It looks as if you're mixing client vs server-side code.

objFile.writeLine(orders(x))

That is VBScript to write to a file, which you can only do on the server.

document.getElementById

This is client-size code that is usually executed in JavaScript. You can use VBScript on IE on the client, but rarely does anyone do this.

On the server you'd usually refer to form fields that were part of a form tag, not DOM elements, (assuming you're using classic ASP) using request("formFieldName").

To make server-side stuff appear on the client (when you build a page) you'd embed it in your HTML like this:

<% = myVariable %>

or like this (as part of a code block):

document.write myVariable
Diodeus
Well, here's the deal: The app I'm creating is for my own personal use. It's not residing on a 'server', but rather sitting on my local hard drive. As a result, both objFile.writeLine(orders(x)) AND document.getElementById work. It's just not recognizing the use of a variable within the ID.
+1  A: 

I was able to get this working. Thanks to both of you for your time and input. Here is what solved it for me:

Rather than using

document.getElementById("order" & x).value

I set the entire ID as a variable:

temp = "order" & x
document.getElementById(temp).value

It seems to be working as expected. Again, many thanks for the time and effort on this!

A: 

Don't you need to change your loop slightly?

For x = 0 To num - 1

E.G. With 4 items you need to iterate from 0 to 3.

Magnus Smith
A: 

hi who can make me a overflow style on a site... i want to overflow is a music playlist... can u make me a overflow playlist that when i move up the its goes up and when i move down the page it move down to... i will just send you the code if you send me a message that you are willing to do it for me send the code to my email.. heres my e-add [email protected] tnx