I have a function that is called when an element is clicked.
function showHide(elem){
var clickedText = elem.getElementsByTagName("p");
clickedText.style.color = "green";
}
When I run this code I get an Uncaught TypeError: Cannot set property 'color' of undefined. However, if I do console.log(clickedText) it logs the appropriate
tag. Can’t for the life of me figure out why this isn’t working, have I missed something totally obvious?
The
getElementsByTagNamemethod returns a collection of elements (in aNodeList, which is like an array).You will need to specify an index. For example:
Currently, you’re trying to access the
styleproperty of theNodeListitself, rather than an element contained within it.