DOM question -- missing attribute

S

Steven Daedelus

Hey all,

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?

stedae
 
B

Berislav Lopac

Steven said:
Hey all,

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?


Probably because 'name' is not an (X)HTML-defined attribute for div, and
therefore it's missing from the object's prototype as defined by the DOM. I
believe getAttribute('name') should do the trick.

Berislav

PS. For an alternative way to check all the properties of the browser,
consider this little function I wrote:

/**
* Help function which iterates through all the properties of an object.
* It returns a textual list containing the name and value of each
property.
*
* @argument object The object which is queried for properties.
* @argument objectName The name of the object (or its prototype).
*/
function GetObjectProperties(object, objectName)
{
var result = '';
for(var i in object) {
result += objectName + '.' + i + ' = ' + object + '\n';
}
return result;
}
 
M

Martin Honnen

Steven Daedelus wrote:

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?


There is a difference between HTML attributes and script properties. An
HTMLDivElement object has certain properties that reflect attributes but
has additional properties not directly reflecting attributes
(parentNode, firstChild, lastChild, childNodes to name only a few). In
general Mozilla doesn't reflect attributes as properties where the
attribute is not part of HTML. And name is not a defined attribute for
<div> elements in HTML 4.
If you want to access attributes as defined in the markup use
getAttribute('name')
on the element object.
 
S

Steven Daedelus

Martin Honnen said:
Steven Daedelus wrote:

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?


There is a difference between HTML attributes and script properties. An
HTMLDivElement object has certain properties that reflect attributes but
has additional properties not directly reflecting attributes
(parentNode, firstChild, lastChild, childNodes to name only a few). In
general Mozilla doesn't reflect attributes as properties where the
attribute is not part of HTML. And name is not a defined attribute for
<div> elements in HTML 4.
If you want to access attributes as defined in the markup use
getAttribute('name')
on the element object.


Ah. That makes sense. Thanks.
 
S

Steven Daedelus

Berislav Lopac said:
Steven said:
Hey all,

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?


Probably because 'name' is not an (X)HTML-defined attribute for div, and
therefore it's missing from the object's prototype as defined by the DOM. I
believe getAttribute('name') should do the trick.

Berislav

PS. For an alternative way to check all the properties of the browser,
consider this little function I wrote:

/**
* Help function which iterates through all the properties of an object.
* It returns a textual list containing the name and value of each
property.
*
* @argument object The object which is queried for properties.
* @argument objectName The name of the object (or its prototype).
*/
function GetObjectProperties(object, objectName)
{
var result = '';
for(var i in object) {
result += objectName + '.' + i + ' = ' + object + '\n';
}
return result;
}


This also makes sense. Thanks.
 
J

Juliette

Steven said:
Hey all,

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?

stedae


Maybe superfluous after the replies already given, but you could also
try:
alert(document.getElementById('myDiv').nodeName);

Good luck, Juliette
 
D

DU

Steven said:
Hey all,

Here's an oddity. Run this through Mozilla:

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<script language="javascript">
at = "";
a = document.getElementById('myDiv').attributes;
for (i=0; i<a.length; i++) {
at = at + a.name + " : " + a.value + "\n";
}
alert(at);
</script>

Mozilla will show you a list of attributes demonstrating that the name
of the DIV is "nippleClamps." However, if you add this line:

alert(document.getElementById('myDiv').name);

It won't return the name. What's up with that?

stedae



The following will work in Mozilla 1.7 RC2, NS 7.1, MSIE 6 and (almost
but not quite) in Opera 7.5 in a HTML 4.01 strict document:

<script type="text/javascript">
function AlertNameAttribute()
{
if(document.getElementById("myDiv").attributes["name"].specified)
{

alert("document.getElementById('myDiv').attributes['name'].nodeValue = "
+ document.getElementById("myDiv").attributes["name"].nodeValue);
};
}
</script>
</head>

<body>

<div id="myDiv" name="nippleClamps" style="display: none">Test</div>

<p><button type="button" onclick="AlertNameAttribute();">Alert the name
attribute</button></p>

DU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top