javascript - Retrieve the parent of a clicked element -


i've got < table > - < tr > - < td > structure. each < td > includes < div > 3 < span > inside. need function, returns clicked < td > every time click either < td > or of elements (< div >, < span >#1, < span >#2, < span >#3).

what tried setting z-index property < td > of higher value 1 < div > , < span > elements. thus, tried "hide" them behind < td >, didn't work.

here's i've got now: http://jsbin.com/mocajahalu/2/edit?html,css,js,output

please, help.

try this...

document.addeventlistener('click',function (e) {    var parentel = null;     if (e.target.tagname === "td") {      // when user clicks on td      parentel = e.target;    } else if (e.target.parentelement.tagname === "td") {      // when user clicks on of child element (the divs , spans)      // if user clicks on child elements      parentel = e.target.parentelement;    }     // parentel td element    alert(parentel);    e.preventdefault(); }, false); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -