Home > front end >  Cannot read properties of undefined (reading 'tagName')
Cannot read properties of undefined (reading 'tagName')

Time:01-16

I don't know why it's returning error with tagName. is subLabel wrong?

for (var ind = 0; ind < target.length; ind  )
    {
        var target = target[ind]
        for (var ind; ind < target.childNodes.length; ind  );
        {
            const subLabel = target.childNodes[ind];
            if (subLabel.tagName == "SPAN" || subLabel.tagName == "LI") //error line
            {
                //do something with label
            }
            typeDisplay(subLabel);
        }  
    }

CodePudding user response:

You are using var ind twice. Use let instead. Read this

CodePudding user response:

It's common for nested loops, to use a different loop variable, such as while the outer loop uses var i=0; the inner loop uses var j=0;.

The posted code, the 2nd var ind is duplicated, because it shares the same variable scope of the 1st var.

CodePudding user response:

You are using the same variable name in both inner and outer for loop (ie. var ind).

what you can do here is either change the variable name in the inner loop or change the variable name in the outer loop.

  •  Tags:  
  • Related