Mareena W
Mareena W

Reputation: 23

For Each... Loop not working

I'm working on incorporating VBScript scripts in my HTML. Basically what I am doing now is having some input boxes appear on page load that asks the user to input four colours which are stored in an array. Later I am having the colours displayed on the page using a function I created. The function works like it is supposed to when I call it for every individual element of the array but my For Each loop doesn't do anything.

Here is the code that collects the colour information from the user:

Option Explicit
Dim colour(4)

colour(0) = Inputbox("Colour number 1")
Msgbox("The colour you entered is: " & colour(0))
colour(1) = Inputbox("Colour number 2")
Msgbox("The colour you entered is: " & colour(1))
colour(2) = Inputbox("Colour number 3")
Msgbox("The colour you entered is: " & colour(2))
colour(3) = Inputbox("Colour number 4")
Msgbox("The colour you entered is: " & colour(3))

This is the script I later call on to display those colours on the page, without the loop, that does exactly what I want it to:

Function printText(words)
    document.write("<h3 class='bodyheader'>Let's add the text '" & words & _
        "' to our website</h3>")
End Function

printText(colour(0))
printText(colour(1))
printText(colour(2))
printText(colour(3))

When I implement my For Each loop I don't get any kind of output at all, that area of the page remains blank. The error I get is

illegal assignment: 'element'

Function printText(words)
    document.write("<h3 class='bodyheader'>Let's add the text '" & words & _
        "' to our website</h3>")
End Function

For Each element In colour
    printText(colour(element))
Next

Is something wrong with my syntax in the loop or is it another issue that I'm missing?

Upvotes: 1

Views: 1742

Answers (1)

Coding Duchess
Coding Duchess

Reputation: 6909

just use:

For Each elm In colour
    printText(elm)
Next

or use:

For i=0 To UBound(colour)
    printText(colour(i))
Next

Upvotes: 1

Related Questions