Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help getting certain elements/ array manipulation

Status
Not open for further replies.

Xaqte

IS-IT--Management
Oct 4, 2002
971
US
I need to get all parent elements of a specified element.
Take the following HTML as an example:
Code:
<div id="main">
[blue]<div>[/blue]
   <div>One</div>
   <div>Two</div>
[blue]</div>[/blue]
[blue]<div>[/blue]
   <div>One</div>
   <div>Two</div>
[blue]</div>[/blue]
</div>

I only need to get the divs marked in blue. Since I couldn't find a way to just pluck these (if you know how this would simplify my process greately), I am doing:

Code:
var holder = document.getElementById("main"); 
var containers = holder.getElementsByTagName("*");

This gives me every element in "main"... so how can I go through "containers" and get just the the ones I want (just the parents)?

I hope I've explained this clearly enough, if not let me know. Any thoughts/experience would greatly be appreciated!

X
 
You could always check the innerHTML of the elements, and if that contained another <div> then it would be an element you're looking for. You might have to adjust for case sensitivity because IE 6.0 changes <div> to <DIV>.

Lee
 
Thanks for the response, Lee!

However, I failed to mention that I'm trying to make my function flexible by not limiting element types.

But, I did just have a brain storm and figured it out:

Code:
for (var i = 0; i < containers.length; i++) {
 if(containers[i].parentNode.id == holder.id) {

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top