The user can't actually click on the .inner
elements because when they try do, what they actually end up clicking on is the child objects in the .inner
objects. As such, you won't get a target with that class.
You have a couple choices. First off, if your HTML isn't dynamically created with JS code, then you could just apply the event listeners directly to all .inner
objects and the normal click event propagation will cause them to trigger when child nodes are clicked on.
Or, if you want to keep the event handler on the body, then the work-around is probably to see if the parent chain contains that class (your target will be several levels below .inner
).
function hasClass(elem, cls) {
var str = " " + elem.className + " ";
var testCls = " " + cls + " ";
return(str.indexOf(testCls) != -1) ;
}
function isTargetInClass(target, cls) {
while (target && target !== document.body) {
if (hasClass(target, cls)) {
return true;
}
target = target.parentNode;
}
return false;
}
function theHandler(e){
e = e || window.event;
var target = e.target || e.srcElement;
if (isTargetInClass(target, "inner")) {
//console.log("inner class was clicked");
alert("inner class was clicked");
}
}