Tuesday, February 8, 2011

VBScript: Using a variable within a DOM element

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!

  • 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?
  • 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
    
    From Diodeus
  • 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!

  • 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.

  • 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 jeric_magante@yahoo.com tnx

0 comments:

Post a Comment