I am writing code to add Event Listeners to all of my images, so that when hovered over, a label will show. The image and its accompanying label are both in the same div, so I'm trying to select all of the pictures first. Then, for each picture, I get the first child of its parent element (the picture's older sibling, the label). Then, I attach an event listener to the picture, with a function passing in the label element, that changes the display property of the label from 'none' to 'inline block'.
My code doesn't throw any errors, but when it runs, it logs 'el: ' without anything after that. What am I doing wrong?
Thank you!
function displayLabel(el) {
console.log("el: " + el)
el.style.display = 'inline block'
}
var autProPics = document.getElementsByClassName("authorprofilepic");
console.log(autProPics)
for (var i = 0; i < autProPics.length; i++) {
var pel = autProPics[i].parentElement
var el = pel.firstElementChild
autProPics[i].addEventListener('mouseenter', function (){
displayLabel(el)
})
}
.authorprofilepic {
display: block !important;
width: 4em !important;
margin: auto;
margin-top: .5em;
}
body {
background-color: rgb(221,221,221);
}
.authorprofile {
padding-top: 1em;
display: inline-block;
width: 5em;
}
.authorprofilelabel {
display: none !important;
margin: auto !important;
max-width: 12em !important;
}
<DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/semantic-ui@2.2.13/dist/semantic.min.css" rel="stylesheet"/>
</head>
<body>
<div class="authorprofile">
<a class="ui basic label authorprofilelabel">Ben Gubler</a>
<a href="#" class="ui image authorprofilepic inline circular">
<img src="https://semantic-ui.com/images/wireframe/square-image.png">
</a>
</div>
<div class="authorprofile">
<a class="ui basic label authorprofilelabel">Ben Gubler</a>
<a href="#" class="ui image authorprofilepic inline circular">
<img src="https://semantic-ui.com/images/wireframe/square-image.png">
</a>
</div>
<div class="authorprofile">
<a class="ui basic label authorprofilelabel">Ben Gubler</a>
<a href="#" class="ui image authorprofilepic inline circular">
<img src="https://semantic-ui.com/images/wireframe/square-image.png">
</a>
</div>
<div class="authorprofile">
<a class="ui basic label authorprofilelabel">Ben Gubler</a>
<a href="#" class="ui image authorprofilepic inline circular">
<img src="https://semantic-ui.com/images/wireframe/square-image.png">
</a>
</div>
<i class="ui plus icon link huge"></i>
</body>
</html>