views:

86

answers:

5

Here is my code:

var divarray = document.getElementById("yui-main").getElementsByTagName("div");
var articleHTML;
var absHTML;
var keyHTML;
var bodyHTML = [];
for( var i in divarray) {
    if(divarray[i].className == "articleBody"){
        articleHTML = divarray[i];
        for( var j in articleHTML ){
            bodyHTML[i] = '';
            if(articleHTML[j].className == "issueMiniFeature"){continue;}
            if(articleHTML[j].className == "abstract"){absHTML = articleHTML[i]; continue;}
            if(articleHTML[j].className == "journalKeywords"){keyHTML = articleHTML[i]; continue;}
            bodyHTML[i] = articleHTML[i];
        }
        break;
    }
    i++;
}

The error I am getting is:

SyntaxError: Unexpected token var

I am using Google Chrome

+5  A: 

The javascript for...in doesn't do what you would expect (which is enumerate through eleemnts in an array.

for...in in javascript will enumerate through the key/value pairs (or public variables) that make up the object (which isn't what you want).

You need to use a good, old fashioned for loop.

Justin Niessner
Ditto what @justin said, here http://www.w3schools.com/js/js_loop_for_in.asp
dscher
+1: I've run into this exact problem. However, I opted to use jQuery's .each loop instead, since it was in a (custom) jQuery plugin used by my employer.
R. Bemrose
Hey, I said it first ... :)
Robusto
A: 

Replace "foreach", with "for" and you're good to go.

teehoo
A: 

Why not use a traditional for loop instead? You're not really using an associative array here ...

Robusto
A: 

That's not the right way to iterate over a collection.

You want a standard for loop, not a for..in loop

for( var i = 0, l = divarray.length; i < l; i++ ) {

There's something else, you then proceed to try to iterate over each element

for( var j in articleHTML ){

articleHTML at this point holds a reference to a single HTML node - not a collection or array of any sort.

Peter Bailey
A: 

I think you mistaking JavaScript for the functionality of PHP. JavaScript does not have foreach loops. JavaScript has for in, which is what you are incorrectly using and normal for loops. Use a standard for loop when dealing with arrays. You will need to use a for in loop with object literals because the index is not the simplicity of an incrementing positive integer.

In JavaScript a for loop has 3 parts in its argument separated by a semicolon as follows:
* start position of incrementor (optional if the variable is previous defined with 0 or a positive integer)
* end position of incrementor
* method of incrementation

In the following examples arrayName is value I made up for the name of an array:

for (; a < arrayName.length; a += 1) {
for (a = x + 1; a < arrayName.length + 3; a += 2) {

The for in loop argument has two required parts and a third part to prevent errors using an if condition:
* The value of an index to search for * The name of the container in which to search * The third part is an if condition

The following example will return the value supplied to the "book" index of the objectName object literal. objectName is a name I made for an example object literal:

for ("book" in objectName) {
    if (objectName.hasProperty("book")) {