0
var arr = new Array();
arr = document.getElementsByClassName("repestyl2");
    var index = 0;
while(index !== arr.length){
    if(arr[index].innerHTML.indexOf("Eck") >= 0){
    arr[index].removeNode(true);    
    arr[index].parentNode.removeChild(arr[index]);
    index++;
    }

Is this code ok? Because I can't get it to work :)
Simple question :P
Thanks :)
Maybe I should use for-each o.O

Edited by desup: foreach

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by desup
0

Here's an annotated version to describe what's going on.

var arr = new Array();//arr is an array.
arr = document.getElementsByClassName("repestyl2");//whoops, now arr is a DOM node. The array has been discarded.
var index = 0;//OK
while(index !== arr.length){//arr is a DOM node, which doesn't have a length property
    if(arr[index].innerHTML.indexOf("Eck") >= 0){//again arr is not an array
        arr[index].removeNode(true);//and again
        arr[index].parentNode.removeChild(arr[index]);//and again
        index++;//ok
    }//ok, end of if clause
}//missing end of while loop

To add element(s) to an array you can do any of the following:

var arr = new Array(v1, v2, v3); //add variables at indexes 0, 1 and 3 
var arr = [v1, v2, v3]; //Same as above.
var arr = [];//new empty array.
//then ...
arr[0] = v1; //set a specific index (0 in this example)
arr.push(v1); //add v1 to the end of the array
arr.unshift(v1); //add v1 to the beginning of the array
arr['name']; //add a named (associative) element

I think that's about it.

Airshow

Edited by Airshow: Format

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.