0

Hello I'm new to javascript, I'm using es6 to my code.

Basically, I have an issue with IE for addEventListener, the idea, when we click the image, pop up appears, it's working on chrome, but it doesn't work in IE. I know there's related topic already about this, ex: addEventListener in Internet Explorer

I tried to implement this, but it doesn't seem to work, I think I need to understand more how to implement it related to my code, if anyone could help, I'll really appreciate this.

const toggleButton = document.querySelector('.jsModalToggle');
    const container = document.querySelector('.modal-yt-container');

    toggleButton.addEventListener('click', _ => {
        document.body.classList.add('modal-yt-is-open')
    })

    container.addEventListener('click', e => {
        if (!e.target.closest('.modal-yt-video')) {
            document.body.classList.remove('modal-yt-is-open')
        }
    })
 .installation-video-callout-text-container {
            padding: 20px;
        }

        .installation-video-callout-text p{
            font-size: 1em;
            line-height: 16px;
        }

        .installation-video-callout-text .green_btn{
            margin: 20px 0 0px 0;
        }

        .installation-video-callout-text h2{
            line-height: 45px;
            font-size: 32px;
        }

        .installation-video-callout-img iframe{
            height: 300px;
        }
 
 
 .modal-yt-container {
        position: fixed;
        display: flex;
        justify-content: center;
        align-items: center;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        opacity: 0;
        z-index: -1;
        background-color: rgba(0, 0, 0, 0.78);
    }

    .modal-yt-is-open .modal-yt-container {
        z-index: 9999;
        opacity: 1;
    }

    .modal-yt-video{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 45%;
    }
<img class="jsModalToggle installation-video-callout-img" src="image" style="cursor:pointer">image click</img>

<div class="modal-yt-container installation-video-callout-img">
        <div class="modal-yt-video">
            <iframe type="text/html"
                    width="100%"
                    height="500px"
                    src="https://www.youtube.com/embed/TA6blZJ6nVw"
                    frameborder="0">
            </iframe>
        </div>
    </div>
Udzzzz
  • 155
  • 9

2 Answers2

1

No version of Internet Explorer supports arrow functions. The same is true for all ES6 Javascript features except const and let.

So this will not work in any IE:

toggleButton.addEventListener('click', _ => {
    document.body.classList.add('modal-yt-is-open')
})

container.addEventListener('click', e => {
    if (!e.target.closest('.modal-yt-video')) {
        document.body.classList.remove('modal-yt-is-open')
    }
})

Instead, use ES5 functions:

toggleButton.addEventListener('click', function(_) {
    document.body.classList.add('modal-yt-is-open')
})

container.addEventListener('click', function(e) {
    if (!e.target.closest('.modal-yt-video')) {
        document.body.classList.remove('modal-yt-is-open')
    }
})

The next problem is that no version of Internet Explorer supports HTMLElement.prototype.closest() either. If you want to use it, you need to polyfill it:

if (!Element.prototype.matches) {
  Element.prototype.matches = Element.prototype.msMatchesSelector || 
                              Element.prototype.webkitMatchesSelector;
}

if (!Element.prototype.closest) {
  Element.prototype.closest = function(s) {
    var el = this;

    do {
      if (Element.prototype.matches.call(el, s)) return el;
      el = el.parentElement || el.parentNode;
    } while (el !== null && el.nodeType === 1);
    return null;
  };
}
connexo
  • 53,704
  • 14
  • 91
  • 128
  • Thank you for the answer, now I understand how it works, I appreciate it, sorry I just have chance to answer – Udzzzz Apr 29 '20 at 22:07
0

It asks you 3 things event, element, function

var event = "click"
var element = document.querySelector('.jsModalToggle');
var myFunc = function(e){

        if (!e.target.closest('.modal-yt-video')) {
            document.body.classList.remove('modal-yt-is-open')
        }
}

then you pass everything to the function

addEvent(event, element, myFunc);
Oscar Rendón
  • 52
  • 1
  • 8