-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
  • 741,623
  • 53
  • 500
  • 612

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
  • 30,962
  • 25
  • 85
  • 135
wuiyang
  • 409
  • 2
  • 5
  • 18
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
  • 586
  • 6
  • 27
  • 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