Requirement: Return the element object.
Problem: Using the code below, I expected the links to return [object], but they actually return the string in the href attribute (or in the case of the first link, the Window object).
(The HTML below has been tested in FireFox 3.6.8 and Internet Explorer 7 (7.0.6002.18005) with the same results.)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Anchor onclick tests</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<a href="javascript:alert(this);" title=""><a href="javascript:alert(this);">...<a/></a> - Returns: [object Window]<br />
<a href="#" onclick="alert(this);" title=""><a href="#" onclick="alert(this);">...<a/></a> - Returns: Full URI<br />
<a href="javascript:void(0);" onclick="alert(this);" title=""><a href="javascript:void(0);" onclick="alert(this);">...<a/></a> - Returns: javascript:void(0);
</div>
</body>
</html>
Adding .tagname
to the this keyword returns undefined
for the first link but correctly identifies the second and third as A
. Likewise, requesting .href
returns undefined
for the first link but correctly outputs the href (full URI in the case of '#').
Does anyone know why, and how I can get a hold on the A
object itself?