Wojciech Maj
Wojciech Maj

Reputation: 1122

TypeError: Array not defined

I'm trying to make a bookmarklet that checks up every row in a table, and if a given row has specific content in one of its cells, it checks the checkbox in that row. However I always end up with a message:

[19:55:11.752] TypeError: rows[a] is undefined

The full code for my bookmarklet:

document.getElementsByName('0.0.7.3.0.9.3.1.1.1.0.13.3.1.1.11.1.3'[0].options[2].selected=true;
var table = document.getElementsByClassName('content-status')[0];
var rows = table.getElementsByTagName('tr');
for(var a=1; a<=rows.length; a++){
    if(rows[a].getElementsByClassName('pricing-tiers')[0].getElementsByTagName('div')[0].innerHTML != 'Invalid Price') {
        rows[a].getElementsByTagName('input')[0].checked=true;
    }
}
document.getElementsByName('0.0.7.3.0.9.3.1.1.1.0.21.2.0.1.1.1')[0].click();

I intentionally start a from 1 since 0rd row is a header row. What am I doing wrong?

Upvotes: 1

Views: 2069

Answers (1)

Blender
Blender

Reputation: 298452

Your for loop is jumping one element too far because of the <=:

for(var a=1; a<=rows.length; a++){
              ^^

Change it to a < and it'll work.

Upvotes: 4

Related Questions