i am trying to choose which element to show and hide using javascript, i can't make change on html, the below javascript show error on "Cannot set property 'display' of undefined". How can i show and hide element which share same class?
var showHideBrandPic = document.querySelectorAll("h1.plp_brand_title");
var pic = document.querySelectorAll('.hero_discription_container_left');
function showHidePic (){
showHideBrandPic.forEach(showHideBrandPic => {
if(showHideBrandPic.textContent === "ADIDAS BY PHARRELL WILLIAMS"){
pic.style.display = "none";
}else {
pic.style.display = "block";
}
})
};
showHidePic();
<h1 class="plp_brand_title">abc</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="600">
<h1 class="plp_brand_title">ADIDAS BY PHARRELL WILLIAMS</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="600">
, but i don't know how to do it. The only thing i can define it need to show or hide is h1 textContent
– Charmaine Ho Jul 01 '21 at 05:51