Thanks to this wonderful post, I came to know that there is a native method in all browsers to check as to whether one element is within another element. This can be very useful for manipulating custom drop downs (like the one you see when you click the More Actions in gmail). Once the custom drop down is opened, it should be closed on clicking anywhere in the page except for the area within the drop down. This involves capturing the mouse click and finding whether the element where the click originated is contained within the drop down element (implemented using a div) or not. I used to do it recursively finding the parent of each element and checking whether it is the custom drop down element or not till the body element is reached because I was ignorant of this wonderful native method. This method makes it so easy and has made my code much more neater and more maintainable.

Advertisements