Browsers will not allow you to farm out document.getElementById like that:
Code:
$ = document.getElementById;
If you are going to use it, just use it. The $ really shouldn't be used in javascript, the fact that jQuery and other libraries do so doesn't make it right.
There are various ways to get the element clicked on when you've attached/added the event.
As long as there is no nesting of elements, this works well:
Code:
function alertText(e){
e = e || window.event;
var t = e.target || e.srcElement;
alert(t.firstChild.nodeValue);
}
A working demo:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function alertText(e){
e = e || window.event;
var t = e.target || e.srcElement;
alert(t.firstChild.nodeValue);
if(e.preventDefault) e.preventDefault();
return false;
}
function myInit(){
var a = document.getElementById('test');
if (window.addEventListener)
a.addEventListener('click', alertText, false);
else if (window.attachEvent)
a.attachEvent('onclick', alertText);
}
if (window.addEventListener)
window.addEventListener('load', myInit, false);
else if (window.attachEvent)
window.attachEvent('onload', myInit);
</script>
</head>
<body>
<a href="#" id="test">What's This?</a>
</body>
</html>
Bookmarks