0

I am having issues figuring out how to get this working with pure Javascript. I know that by using document.getElementsByTagName('img') I will need to loop through each instance of an image.

I am having no luck with everything I have tried. So far, I am just trying to test with the first image only.

var change = document.getElementsByTagName('img');
        console.log(change);
        change[0].onmouseover = function() {mouseOver()};

        change[0].onmouseout = function() {mouseOut()};
        function mouseOver() {
            change[0].style.opacity = '0.6';
        }

        function mouseOut() {
            change[0].style.opacity = '1';
        }
partyboi
  • 17
  • 5
  • `for (const img of ...) { img.onmouseover = () => img.style.opacity = '0.6';` etc – CertainPerformance Nov 12 '19 at 03:40
  • You also need to add the `mouseout` listener – CertainPerformance Nov 12 '19 at 04:22
  • Sounds like you don't have any images in the DOM when your code runs. – CertainPerformance Nov 12 '19 at 04:24
  • @CertainPerformance I am assuming this would be equivalent. `for(const img of document.getElementsByTagName('img')) {img.onmouseover = () => img.style.opacity = '0.6';}` My problem with this, is it does not output anything. I have even added to my original script for a testing the constant, with `const nodeItems = Array.from(change);` then trying to `console.log(nodeItems` returns no values. – partyboi Nov 12 '19 at 04:24
  • Ok, sorry. I was editing the comment to give it more detail. I will check into that. Thanks. – partyboi Nov 12 '19 at 04:25

0 Answers0