Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Finding the children from getElementsByTagName
Old 04-29-2007, 12:12 PM Finding the children from getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Hi...

First, Tripy thank you for my other posting.

Now, I came across ANOTHER problem...

Here's the HTML of what I need to manipulate...

<div id="menu1">
<span class="dropdown">
<a href="#">Equities &amp; Options</a><br />
<a href="#">Fixed Income</a><br />
<a href="#">Mutual Funds</a><br />
<a href="#">Managed Money</a><br />
<a href="#">Annuities</a><br />
<a href="#">Other Investments</a><br />
<a href="#" class="dropdown">Account Types</a><br />
</span>
</div>

I have to delete the ENTIRE line of the menu... the <a> and <br />

To do this, I'm pulling getElementsByTagName("span")...
var spanList = getElementsByTagName("span");

This spanList length is 1 because there's only one span.

My problem is accessing the child. If I hard code it (spanList[i].childNodes[x].nodeName - where x = a number in the array), I can access it.

But, how to do via a for loop.

hasChildNodes() does not work on getElementsByTagName, only on getElementsById.

if I do a spanList.hasChildNodes() - I get a JavaScript error saying the object doesn't support this.

I've scoured the web and books but unless if I pre-know the number of child elements in spanList, I can't access them.

Thanks
Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-29-2007, 02:09 PM Re: Finding the children from getElementsByTagName
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Hi Donnaz,
Quote:
First, Tripy thank you for my other posting.
You're welcome.

You have to split your logic deeper.
Here, you don't want to get an array of element to delete, you want to get the element who's childs must be deleted.
Then, loop through every childs and remove them one by one.

Just to make it simpler, I assume you can put an ID on the span with the class "dropdown".
Code:
<div id="menu1">
<span class="dropdown" id="dropDown">
<a href="#">Equities &amp; Options</a><br />
<a href="#">Fixed Income</a><br />
<a href="#">Mutual Funds</a><br />
<a href="#">Managed Money</a><br />
<a href="#">Annuities</a><br />
<a href="#">Other Investments</a><br />
<a href="#" class="dropdown">Account Types</a><br />
</span>
</div>
<script type="text/javascript">
  var contener=document.getElementById('dropDown');
  while(contener.childNodes.length>0){
    contener.removeChild(contener.firstChild);
  }
</script>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-29-2007, 03:58 PM Re: Finding the children from getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Tripy,

Thanks.

I can't put in an id. This is a code snippet of a file that is read in by like 15,000 pages, so if I put in an id, I'm not sure if it will screw up code somewhere else that maybe calling for the same id.

That's the problem.

I will try what you suggest. I just have to finish this.

DonnaZ
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 04:16 PM Re: Finding the children from getElementsByTagName
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No problem.
This should do the trick then:
We target the div with an id, and we take the first child (the span then) as the container to empty.

Code:
<div id="menu1">
<span class="dropdown">
<a href="#">Equities &amp; Options</a><br />
<a href="#">Fixed Income</a><br />
<a href="#">Mutual Funds</a><br />
<a href="#">Managed Money</a><br />
<a href="#">Annuities</a><br />
<a href="#">Other Investments</a><br />
<a href="#" class="dropdown">Account Types</a><br />
</span>
</div>
<script type="text/javascript">
  var _top=document.getElementById('menu1');
  var container =_top.firstChild;
  while(container .childNodes.length>0){
    container .removeChild(contener.firstChild);
  }
</script>
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 04-29-2007 at 04:18 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-29-2007, 04:21 PM Re: Finding the children from getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Tripy,

I'm not sure on your code. It deletes ALL the menu items.

If I do slap on an id, how would I access the child nodes individually? I'm trying to kill the Annuities part of it with it's <br /> (because they're two separate elements).

There's also a problem between browsers. Mozilla adds text nodes so it throws off the count on the index. (I would test for the condition though).

This really is a pain because it's for my job and I've been working on it for three days.

Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 04:49 PM Re: Finding the children from getElementsByTagName
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I must have misunderstood you then.
Effectively, this script deletes everything inside the <span>
Quote:
I have to delete the ENTIRE line of the menu... the <a> and <br />
Can you be more precise ? I don't get the subtility about what you want to do.

But, if I detail your first post:
Quote:
hasChildNodes() does not work
Well, as I showed you in my code, you can do a childNodes.length to get the number of childs in a element.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-29-2007, 08:57 PM Re: Finding the children from getElementsByTagName
Extreme Talker

Posts: 169
Trades: 0
I think the base of the problem is how the menu is structured, i.e. as a span instead of something simpler like an unordered list.
I don't actually know how to do this out of my head, otherwise I'd gladly assist you, but, tripy, what I think DonnaZ is trying to do is the following. Menu before deletion
Code:
<div id="menu1">
<span class="dropdown">
<a href="#">Equities &amp; Options</a><br />
<a href="#">Fixed Income</a><br />
<a href="#">Mutual Funds</a><br />
<a href="#">Managed Money</a><br />
<a href="#">Annuities</a><br />
<a href="#">Other Investments</a><br />
<a href="#" class="dropdown">Account Types</a><br />
</span>
</div>
Menu after deletion (of Mutual Funds)
Code:
<div id="menu1">
<span class="dropdown">
<a href="#">Equities &amp; Options</a><br />
<a href="#">Fixed Income</a><br />

<a href="#">Managed Money</a><br />
<a href="#">Annuities</a><br />
<a href="#">Other Investments</a><br />
<a href="#" class="dropdown">Account Types</a><br />
</span>
</div>
As in not only remove the anchor, but also the line break element. Not sure if this element can even be accessed via JS.
__________________
George Bush would never take me alive.
MJM_RDS is offline
Reply With Quote
View Public Profile
 
Old 04-30-2007, 08:59 PM Re: Finding the children from getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Tripy and all,

Thanks.

I did slap an ID on it... it just was the best way of doing it.

Pulling the getElementsByTagName("a") would leave a <br>

So, I just slapped an ID on the span. Then I was able to kill both the <a> and the <br>

I actually did something cool that only worked in IE... but because of the extra #text tags... it would never work in Mozilla.

Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 05-01-2007, 06:39 AM Re: Finding the children from getElementsByTagName
Extreme Talker

Posts: 169
Trades: 0
Quote:
Originally Posted by DonnaZ View Post
I actually did something cool that only worked in IE... but because of the extra #text tags... it would never work in Mozilla.
I wonder what that could be
__________________
George Bush would never take me alive.
MJM_RDS is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Finding the children from getElementsByTagName
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28780 seconds with 12 queries