Wednesday, June 27, 2012

How to detect clicked on outside of a particular div?

document.onclick = clickEvent;
function clickEvent(e) {
    var targ;
    if (!e) var e = window.event;

    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;

    //if (targ.nodeType == 3) // defeat Safari bug

        if (targ.nodeName != "HTML") {
        while (targ.nodeName != "BODY") {
            if (targ.id && targ.id == 'divGrid') {
                return false;
            } else {
                targ = targ.parentNode;
            }
        }
    }    //This is the place where you need to write code when you click outside of the div.
}

No comments:

Post a Comment