Home > Enterprise >  Cannot access textContent of HTML el but can console log it
Cannot access textContent of HTML el but can console log it

Time:02-28

so i am trying to manipulate with table on this website.

my script i insert in the chrome/firefox console on the website:

let skins = [
"button" //simplified 
];

skins.forEach((skin)=>{

document.querySelector("tbody").childNodes.forEach((k,i)=>{
if(i>0){ // starts with 1 because i==0 is #text
    
    console.log(k.childNodes[1].textContent); // this works in chrome,firefox
    
    let h = k.childNodes[1].textContent; 
    if(h == "shades") console.log("yes"); // this part doesnt work - error 
}
})

});

when i console.log it, it does work, and show all the <tr> on position 1. But when i try to do some code with if statement, the error message shows up,

 Cannot read properties of undefined (reading 'textContent')
at <anonymous>:11:30
at NodeList.forEach (<anonymous>)
at <anonymous>:8:44
at Array.forEach (<anonymous>)
at <anonymous>:6:7

CodePudding user response:

You are missing {}

if(h == "shades") console.log("yes")

do instead:

if (h == 'shades') {
 console.log("yes");
}

CodePudding user response:

Sorry, i didnt carefully set the boundaries, thats why i get an error. There was another #text and the results also needed .trim() to remove extra whitespace.

  • Related