Reputation: 15906
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
Upvotes: 0
Views: 2161
Reputation: 2871
You can add this to your script:
Array.prototype.foreach = function (callback) {
for (var i=0; i < this.length; i++) {
callback(this[i]);
}
}
Then you simply do this:
myarray.foreach(function (currentItem) {
/*...do whatever with the currentItem...*/
});
Upvotes: 1
Reputation:
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")) {
Upvotes: 0
Reputation: 105916
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.
Upvotes: 0
Reputation: 31893
Why not use a traditional for loop instead? You're not really using an associative array here ...
Upvotes: 0
Reputation: 245479
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.
Upvotes: 4