Harshana
Harshana

Reputation: 195

splice method not working

I am getting all the input values of text boxes. Then store all the input values to an array. If the input type is button it should be removed from the array. This is my code.

<!-- language : java script -->    
function nullchecaking(){

    var arr = new Array();
    arr = document.getElementsByTagName('input');
    var a
    for(a=0; a<arr.length;a++){

        if(arr[a].type == "button"){
            alert("found button");
            arr.splice(a, 1);
            alert(arr);
            a=a-1;  
        }
    }

}

splice method not working. How can i solve this problem ?

Upvotes: 1

Views: 1310

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83358

I think the problem is that getElementsByTagName returns an array-like object, not an array, so you can't call array methods on it. But there's a simple workaround:

arr.splice(a, 1);

would become

Array.prototype.splice.call(arr, a, 1);

EDIT

Sorry - it seems as though the proper way to remove items from what's returned by getElementsByTagName—which is a NodeList—is to use removeChild as described in this answer

Upvotes: 2

Related Questions