just the elements that are one level down?

P

pbd22

Hi.

How do I get all the elements that are "just" one level down from the
parent element? I tired:

var mycont = document.getElementById('mycontent');
var litems = mycont.getElementsByTagName("div");

hoping that this would produce all the div elements that are
just below the parent element but it seems to return "all" the
div elements contained by the parent element.

Below is the meat of the method. I want to hide all div elements
that are not the element clicked on by the user (using display:none).

var mycont = document.getElementById('mycontent');
var litems = mycont.getElementsByTagName("div");

for (var i = 0, l = litems.length; i < l; i++)
{
var li = litems;

if (li.style.display == "")
{
li.style.display = "none";
// alert(li.id);
}

}

what this currently does is completely hide all the elements contained
in "mycontent".

Thanks for your help.
 
J

jidixuelang

Hi.

How do I get all the elements that are "just" one level down from the
parent element? I tired:

var mycont = document.getElementById('mycontent');
var litems = mycont.getElementsByTagName("div");

hoping that this would produce all the div elements that are
just below the parent element but it seems to return "all" the
div elements contained by the parent element.

Below is the meat of the method. I want to hide all div elements
that are not the element clicked on by the user (using display:none).

var mycont = document.getElementById('mycontent');
var litems = mycont.getElementsByTagName("div");

for (var i = 0, l = litems.length; i < l; i++)
{
var li = litems;

if (li.style.display == "")
{
li.style.display = "none";
// alert(li.id);
}

}

what this currently does is completely hide all the elements contained
in "mycontent".

Thanks for your help.


Are you sure??

It works fine at here.

Code:
<div id="mycontent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>

<script type="text/javascript">
var mycont   = document.getElementById('mycontent');
var litems   = mycont.getElementsByTagName("div");

for (var i = 0, l = litems.length; i < l; i++)
{
var li = litems[i];


if (li.style.display == "")
{
li.style.display = "none";
// alert(li.id);
}


}
</script>


Show me your HTML code about "#mycontent",please!
Maybe someone can catch the real error!
 
T

Thomas 'PointedEars' Lahn

pbd22 said:
How do I get all the elements that are "just" one level down from the
parent element? I tired:

var mycont = document.getElementById('mycontent');
var litems = mycont.getElementsByTagName("div");

hoping that this would produce all the div elements that are
just below the parent element but it seems to return "all" the
div elements contained by the parent element.

As specified in W3C DOM Level 2 Core.

You may use XPath --

/**
* @see http://pointedears.de/scripts/types.js
*/
function isMethodType(s)
{
return /\b(function|object)\b/i.test(s);
}

if (isMethodType(typeof document.evaluate) && document.evaluate)
{
var a = document.evaluate("*", mycont, null, 7, null);
}

-- or you may filter the childNodes collection:

else
{
var a = new Array();

for (var i = 0, cn = mycont.childNodes, len = cn.length; i < len; i++)
{
if (cn.nodeType == 1)
{
a.push(cn);
}
}

a.snapshotItem = function(i)
{
return this;
};

a.snapshotLength = a.length;
}

And then iterate over the collection/array (if you don't attempt XPath, you
don't need to build the array):

for (var j = 0, len = a.snapshotLength; i < len; i++)
{
... a.snapshotItem(i) ...
}


PointedEars
 
T

Thomas 'PointedEars' Lahn

How do I get all the elements that are "just" one level down from the
parent element? [...]

Are you sure??

It works fine at here.

Code:
<div id="mycontent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>

<script type="text/javascript">
var mycont   = document.getElementById('mycontent');
var litems   = mycont.getElementsByTagName("div");

for (var i = 0, l = litems.length; i < l; i++)
[...][/QUOTE]

It works for you because your subtree is only one level deep.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
for (var j = 0, len = a.snapshotLength; i < len; i++)
{
... a.snapshotItem(i) ...
}

for (var j = 0, len = a.snapshotLength; j < len; j++)
{
... a.snapshotItem(j) ...
}
 

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
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top