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
Deleting items with getElementsByTagName
Old 04-27-2007, 06:11 PM Deleting items with getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Hi there...

I'm dealing with some very old Javascript where a menu was created with a list of <a hrefs> enclosed in a span tag....

For example

<div id="menu1">
<span class="menu">
<a href="site1">Site One</a>
<a href="site2">Site Two</a>
<a href="site3">Site Three</a>
</span>
</div>

I need to delete "Site Two" from the list. What I did was to getElementsByTagName("a") to grab the anchors.

Thus...

var productList = document.getElementById("menu1");
var spanList = productList.getElementsByTagName("a");

I'm then able to access the text nodes with...

spanList[i].firstChild.nodeValue

Because the firstChild's node is a text value.

I did this in a while loop...

var i = 0;

while (i < spanList.length)
{
if (spanList[i].firstChild.nodeValue == "Site Two")
{
//write code to kill it;
break;
}
i++;
}

I'm able to find the correct line. The problem is... how do I kill the node?

I like working with getElementById better but I can't slap an id on the menus because there's like 50 files with this syntax.

Any suggestions?

Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-27-2007, 07:54 PM Re: Deleting items with getElementsByTagName
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Use removeChild:
Code:
   if (spanList[i].firstChild.nodeValue == "Site Two"){
  //write code to kill it;
  spanList.removeChild(i);
   }
I've typed this from memory, could use some testing...
I'm particulary not sure IE/safari/opera will handle this correctly.

http://developer.mozilla.org/en/docs...nt.removeChild
__________________
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, 06:13 PM Re: Deleting items with getElementsByTagName
Super Talker

Posts: 116
Trades: 0
Tripy,

Thanks for your help! I actually got it ... I took a nap and was able to see the code clearer.

But the solution was exactly as you said. I searched by the text node and in addition, I added one to the index to kill off the extra <br />

I am going to add the id="dropDown" because it makes life oh so much easier.

If it bombs, then I'll go back to getElementsByTagName("span") but at least I know where I'm going now.


Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 06:54 PM Re: Deleting items with getElementsByTagName
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Sometime, you just need to get some sleep. I know it too.
Should be sleeping already though... meeting at 8 tomorrow..

Anyway...
In your case, the process of adding an id to the element should be failsafe. As it don't have one, I don't see how it could interfere.

Just take precautions to avoid giving this element an id already taken, and everything will be fine.
__________________
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!
 
Reply     « Reply to Deleting items with 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.13937 seconds with 12 queries