namedItem() in IE5/6

B

Bzi99

Hi!

Since my blog host doesn't permit me to use a <div id="name"> but
only <div class="name">, I had to change a JS I had from:

document.getElementsById("name").style.display='';

to the cumbersome:

document.getElementsByTagName("div").namedItem("name").style.display='';

This works great in Firefox but not in IE5/6. Does anyone know of an
alternative to namedItem() in weird world of IE DOM?

Thanks in advance,

Rui C. P.
 
A

ASM

En réponse à Bzi99 qui nous a susurré, en date du : 21/07/07 18:32, le
message sibyllin suivant :
Hi!

Since my blog host doesn't permit me to use a <div id="name"> but
only <div class="name">, I had to change a JS I had from:

document.getElementsById("name").style.display='';

to the cumbersome:

document.getElementsByTagName("div").namedItem("name").style.display='';

This works great in Firefox but not in IE5/6.

It is strange that could work in FF because, only Id or name attributes
are inspected (or can be inspected) by namedItem()
<http://www.zvon.org/xxl/DOM2reference/Output/HTML/method_namedItem_HTMLCollection.html>
It is not told that it is to use with other attributes than 'id' or 'name'

However M$ says it is available with IE6 :
http://msdn2.microsoft.com/en-us/library/ms536634.aspx
and to add :
with :
"all, anchors, applets, areas, boundElements, cells, elements, embeds,
forms, images, links, mimeTypes, options, plugins, rows, scripts,
tBodies, FORM, filters, frames, imports, styleSheets, TextRange,
TextRectangle"
where I don't find 'div'

so ... if you don't need IE < 6 you could try :

function getDiv(named) {
return document.all? document.all.namedItem(named) :
document.getElementsByTagName("div").namedItem(named);
}

but ...
that has not to work (no more than your code would have).

Does anyone know of an
alternative to namedItem() in weird world of IE DOM?

Not me.


function getElementByClassName(nameClass) {
var D = document.getElementsByTagName("div");
for(var i=0; i<D.length; i++)
if(D.className == nameClass) return D;
}

getElementByClassName('name').style.display='';


That means you use only once the css class 'name'
 
B

Bzi99

Hi!

Thanks for replying.
function getDiv(named) {
return document.all? document.all.namedItem(named) :
document.getElementsByTagName("div").namedItem(named);

}
Does anyone know of an
alternative to namedItem() in weird world of IE DOM?

Not me.

function getElementByClassName(nameClass) {
var D = document.getElementsByTagName("div");
for(var i=0; i<D.length; i++)
if(D.className == nameClass) return D;

}
getElementByClassName('name').style.display='';



Unfortunately both functions didn't work (at least in IE5 and IE6).

Rui C. P.
 
A

ASM

En réponse à Bzi99 qui nous a susurré, en date du : 21/07/07 20:21, le
message sibyllin suivant :
Unfortunately both functions didn't work (at least in IE5 and IE6).

Unlovely I can't do more because at least the second one works fine with
each of my browsers whom IE-5.2(Mac)
And I really do not understand why it wouldn't work with IE Windows.

test (copy+paste and try) :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>to get a div by its class name</title>
<script type="text/javascript">
function getElementByClassName(nameClass) {
var D = document.getElementsByTagName("div");
for(var i=0; i<D.length; i++)
if(D.className == nameClass) return D;
}
function showHide(what) {
var a = getElementByClassName(what).style;
a.display = a.display==''? 'none' : '';
}
</script>
</head>
<body>
<p>| <a href="javascript:showHide('truc')">show/hide TRUC</a> |
<p>| <a href="javascript:showHide('name');">show/hide NAME</a> |
<div class="truc">div truc</div>
<div class="name">div name</div>
</body>
</html>

or try it directly here :
http://cjoint.com/data/hvxyW2rjdY_getDivByClassName.htm
 
R

Richard Cornford

Bzi99 said:
Since my blog host doesn't permit me to use a <div id="name">
but only <div class="name">, I had to change a JS I had from:

document.getElementsById("name").style.display='';

to the cumbersome:

document.getElementsByTagName("div").namedItem("name").
style.display='';

This works great in Firefox

That does not seem credible as CLASS attributes have nothing to do with
referencing elements by name or ID.
but not in IE5/6. Does anyone know of an
alternative to namedItem() in weird world of IE DOM?

The specification (W3C CORE DOM Level 2) says that the -
getElementsByTagName - method returns an object implementing the -
NodeList - interface, and a - NodeList - does not have a - namedItem -
method. It may be the case that in some environments the -
getElementsByTagName - actually returns an object implementing either
the- NamedNodeMap - or the - HTMLCollection - interfaces, and doing so
is completely acceptable as either of those interfaces includes all the
properties and methods of the - NodeList - interface (that is, any
object implementing either is also implementing - NodeList - (in class
base languages they would be sub-classes of - NodeList -)), but it is a
mistake to expect to be getting an object with a - namedItem - method as
the result of calling - getElementsByTagName. Internet Explorer cannot
be faulted for giving you precisely what the specification says it
should be giving you.

Richard.
 
B

Bzi99

Hi, Stephane!
<p>| <a href="javascript:showHide('truc')">show/hide TRUC</a> |
<p>| <a href="javascript:showHide('name');">show/hide NAME</a> |
<div class="truc">div truc</div>
<div class="name">div name</div>

Great, that worked! The weird thing is: it doesn't work if you use
<div name='name'> instead.
Oh well, problem solved anyway :)

Rui C. P.
 
A

ASM

En réponse à Bzi99 qui nous a susurré, en date du : 22/07/07 11:33, le
message sibyllin suivant :
Hi, Stephane!

Great, that worked! The weird thing is: it doesn't work if you use
<div name='name'> instead.

Do you know what you want or not ?
It was asked divs by their class name, no ?

For any attribute in divs elements ==>

function getElementByAttributeValue(sthng) {
var D = document.getElementsByTagName("div");
for(var i=0; i<D.length; i++) {
var A = D.attributes;
for(k in A) if(A[k].value == sthng) return D;
}
}
function showHide(what) {
var a = getElementByAttributeValue(what).style;
a.display = a.display==''? 'none' : '';
}
Oh well, problem solved anyway :)

and ... could we be informed of your solution ?
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top