views:

215

answers:

7

What's wrong with this code?

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

This is the error I am getting:

ReferenceError: array is not defined

I am using Google Chrome if it helps any.

+2  A: 

It's [] in ECMAScript; this isn't PHP. The interpreter is right - array is not defined, which is why you're getting that.

meder
+6  A: 

That's not how to declare variables as an empty array. You should be using:

var articleHTML = [];

See this previous question for reasoning of using this method instead of new Array()

Chad Birch
+7  A: 

It's not php - you should use

var variable_name = new Array()

or even better

var variable_name = []
Andris
A: 

var articleHTML = new Array();

derek
+1  A: 

Instead of

var articleHTML = array();

and

var bodyHTML = array();

do

var articleHTML = [];

and

var bodyHTML = [];
RedFilter
A: 

You first need to define

var divarray = new array();

Pinu
Javascript is case sensitive. Array will work, array won't.
Matt
A: 

You also don't need to use var six times, you can do:

var divarray = document.getElementById("yui-main").getElementsByTagName("div"),
    articleHTML = [],
    absHTML = [],
    keyHTML = [],
    bodyHTML = [],
    i = 0;

Which works just as well as your six vars but looks much nicer.

Also there are a number of compelling reasons not to use new in instantiate an array (besides []; is much shorter than new Array();)

IndieInvader