Click to See Complete Forum and Search --> : Get tag-type by id
michelle
09-16-2003, 09:53 AM
Can I get the tag-type of an element that I have an ID for?
Like this:
<script>
function getTagTypeById(uid) {
// here is the problem
return document.getElementById(uid).tagType;
}
</script>
<td id="myTd">
<a href="#" id="myHref">a link</a>
</td>
<a href="#" onClick="getTagTypeById('myHref');">This should alert "a"</a>
<a href="#" onClick="getTagTypeById('myTd');">This should alert "td"</a>
Any thoughts?
// Michelle
Charles
09-16-2003, 10:03 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
onload = function () {alert(document.getElementById('fee').tagName); alert(document.getElementById('fie').tagName)}
// -->
</script>
<ul>
<li id="fee"><a href="http://www.w3.org" id="fie">W3CM</a></li>
</ul>
Khalid Ali
09-16-2003, 10:04 AM
Yes, DOM gives you extreme control over the document elements.
document.getElementById(uid).nodeName
will return the name of the node such as
if the node is a <div....
you will get
DIV
Charles
09-16-2003, 10:08 AM
I stand corrected. What I posted above works in MSIE but it would appear that I mis-read the DOM specification. Use "nodeName" instead of "tagName".
Khalid Ali
09-16-2003, 10:21 AM
Hey Charles you are correct as well,
nodeName is Node attribute
http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247
while
tagName is Elements interfaces Attribute.
http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-745549614
The problem is (probably) in the implementation in different browsers
Charles
09-16-2003, 11:04 AM
It's almost as if the DOM was written by a committee.