0

I am trying to access the words from the className muichip label.

I returned 14 results which are undefined in the console log. But I am not to sure why they are not returning back as a string? Can anyone help me?

 const unitTests = unitTestTemplates.map((item) => {
        console.log(document.getElementsByClassName("MuiChip-label").innerHTML)
          
      }
   
bloom bloom
  • 131
  • 8
  • Possible duplicate, question already answered https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return – corashina Sep 17 '20 at 09:50

1 Answers1

1

document.getElementsByClassName() return a list of elements (doc), so you should iterate through that list

document.getElementsByClassName("MuiChip-label").forEach(function(element) {
  console.log(element.innerHTML)
})
hgb123
  • 13,869
  • 3
  • 20
  • 38
  • Great, thankyou! Say if I wanted to check if the text contains a word, how should i do this? Would doing .includes work? – bloom bloom Sep 17 '20 at 03:11
  • @bloombloom if you want to check text only, regardless of the tag, then you should use `element.innerText.includes(...)` ([doc](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText)) – hgb123 Sep 17 '20 at 03:14