...

View Full Version : DOM Attribute Collection



danNL
01-24-2006, 03:15 PM
I've been working on a dynamic script using the DOM and wanted a routine that would parse out the element attribute collection. I came up with a routine that works in FireFox 1.5 and Opera 8, but it doesn't work in IE 6. Any ideas?

Thanks in advance for your help,
Dan


<html>
<head>
<title>DOM Element Properties</title>
<script language="javascript" type="text/javascript">
function objProp(x) {
elem = document.getElementById(x);
document.write(elem.attributes.length + "<br>");
for (i=0;i<elem.attributes.length;i++) {
if (elem.attributes[i].specified) { // to handle IE's 'user-specified' attribute collection
document.write(elem.attributes[i].name + "->" + elem.attributes[i].value + "<br>");
}
}
}
</script>
</head>
<body>
<img src="http://www.codingforums.com/images/icons/icon2.gif" alt="button0" width="16" height="16" border="0" id="btn0" onClick="objProp('btn0');"/> Click the arrow.
</body>
</html>

doomchan
01-25-2006, 10:13 AM
Using the attributes array instead
http://wsabstract.com/dhtmltutors/domattribute2.shtml

danNL
01-25-2006, 11:24 AM
I thought I was using the attributes array ... elem.attributes[i].name and elem.attributes.value ... is this not true?

doomchan
01-25-2006, 03:34 PM
ie and ff work

<html>
<head>
<title>DOM Element Properties</title>
<script language="javascript" type="text/javascript">
function objProp(x) {
var ieattr=0 //for ie
elem = document.getElementById(x);
for (i=0;i<elem.attributes.length;i++) {
if (elem.attributes[i].specified) {
ieattr++;//for ie
document.write(elem.attributes[i].name + "->" + elem.attributes[i].value + "<br>");

}
}
if (document.all){
document.write(ieattr);
}
else{
document.write(elem.attributes.length + "<br>");
}

}
</script>
</head>
<body>
<img src="http://www.codingforums.com/images/icons/icon2.gif" alt="button0" width="16" height="16" border="0" id="btn0" onClick="objProp('btn0');"/> Click the arrow.
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum