1

Code:

for(int j=0; j<virtualFields.length();j++)
{
    finalOptions += '<option value="'+virtualFields[j]+'"><\/option>' ;   
}

Firebug is giving this error:

Missing ; after for loop initializer.

0

2 Answers 2

8

Javascript doesn't understand int. Use var instead.

Some other pointers:

  • Assuming virtualFields is an ordinary array, length is a property, not a method.
  • You may need to html escape the virtualFields[j] values that you're concatenating like that.
  • You only need to escape / inside a regular expression, not within an ordinary string. So </option> would work just fine.

(these points are not related to the error, but you may wish to take them into account anyway)

Sign up to request clarification or add additional context in comments.

8 Comments

Re escaping /: some HTML parsers think that every closing tag, i.e., everything that starts with </, is the closing tag of the script element. That's also why it is advised to either escape those tags appearing within strings in JavaScript, or split the string like this: "<"+"/end>".
Marcel: That sounds interesting, I've never heard that before. Can you provide a reference? What browsers or parsers are you talking about?
And if you separate the "</sc"+"ript>" string it will trigger the alert("hi"); jsfiddle.net/6QE56/1
@BrunoLM: that's a <script> tag, I'm well aware that breaks. It's because once you open the tag, the HTML parser looks only for a closing </script> tag, ignoring everything else. It finds that "inside the JS string", but the parser won't care about whether it's in a string or not (it doesn't understand Javascript). The same thing does not happen with other closing tags, hence why I questioned Marcel's assertion that it happened for every closing tag.
Gijs, you should put an at-sign in front of my first name to let me know you replied to me (so I'll get notified).
|
-1

You are using int to declare a variable.

Use var instead.

Also, if virtualFields is an Array, you should just be accessing the length property, not as a method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.