You can do it in POJS like so, this should be cross-browser compatible and doesn't use any 3rd party libraries. Other advantage is that there is only one event listener per element of the named class, uses event propagation.
Javacsript
/*jslint maxerr: 50, indent: 4, browser: true */
/*global alert */
(function () {
"use strict";
function addEvent(elem, event, fn) {
if (typeof elem === "string") {
elem = document.getElementById(elem);
}
function listenHandler(e) {
var ret = fn.apply(null, arguments);
if (ret === false) {
e.stopPropagation();
e.preventDefault();
}
return ret;
}
function attachHandler() {
window.event.target = window.event.srcElement;
var ret = fn.call(elem, window.event);
if (ret === false) {
window.event.returnValue = false;
window.event.cancelBubble = true;
}
return ret;
}
if (elem.addEventListener) {
elem.addEventListener(event, listenHandler, false);
} else {
elem.attachEvent("on" + event, attachHandler);
}
}
function whichElement(e) {
var target = e.target;
if (target.tagName === "A" && target.parentElement.tagName === "LI" && target.parentElement.parentElement.className === "list-class") {
alert("The " + target.firstChild.nodeValue + " Link has been clicked");
}
}
addEvent(document.body, "click", whichElement);
}());
On jsfiddle
If you were using some newer/custom HTML tags or XML then you may need to consider tagName case sensitivity, and write the following to be certain.
if (target.tagName.toUpperCase() === "A" && target.parentElement.tagName.toUpperCase() === "LI" && target.parentElement.parentElement.className === "list-class") {
In jquery terms the above could be written as
Javascript
$(document).on('click', '.list-class>li>a', function (e) {
alert("The " + e.target.firstChild.nodeValue + " Link has been clicked");
});
On jsfiddle
In jquery they call this event delegation.