-1

Who can help me make this code without JQuery because I don't know how to make this without JQuery.

missClick(event){

    const parentDiv = $(event.target).parent();

    $('body').off().on('click', (e) => {
        if (parentDiv.find(e.target).length === 0) {
            alert('miss click');
            $('body').off();
        }
    });
}
Barmar
  • 669,327
  • 51
  • 454
  • 560

2 Answers2

1

assuming you have use addEventListener('click', missClick)

function missClick(event){
    const parentDiv = event.target.parentElement;

    document.body.removeEventListener('click', missClick);

    const handler = (e) => {
        if (parentDiv.contains(e.target)) {
            alert('miss click');
            document.body.removeEventListener('click', handler);
        }
    };
    document.body.addEventListener('click', handler);
}
Dharman
  • 26,923
  • 21
  • 73
  • 125
wuiyang
  • 390
  • 1
  • 4
  • 16
0

This website seems to be what you are looking for :) http://youmightnotneedjquery.com

example off() looks more like el.removeEventListener(eventName, eventHandler);

$('body') would look more like .getElementByTagName() or document.querySelectorAll('whatever selector');

Jacob
  • 557
  • 5
  • 25
  • My answer is hopefully more of a "teach a person to fish moment" rather than just giving you an answer :) – Jacob Jul 23 '20 at 21:18