Skip to content

Instantly share code, notes, and snippets.

View fransstudio2's full-sized avatar

Fransiskus Winata fransstudio2

View GitHub Profile
@fransstudio2
fransstudio2 / clickOutside.ts
Created April 30, 2020 15:30
Click Outside
export function addClickOutside(el: HTMLElement, fun: Function) {
const elWithFun: HTMLElement & {
clickOutsideFun?: (this: Document, ev: MouseEvent) => any;
} = el;
elWithFun.clickOutsideFun = function (this: Document, event: MouseEvent) {
if (!el.contains(event.target as Node)) {
fun(event);
}
return true as any;
};
@fransstudio2
fransstudio2 / parents.ts
Created April 30, 2020 15:31
Simulate jQuery.parents(containerClass)
let containerClass = "containerClass";
let insideContainer = false;
let elem = event.target;
while (elem) {
if (elem.classList && elem.classList.contains(containerClass)) {
insideContainer = true;
break;
}
elem = elem.parentNode;
}