Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-16-2011, 09:00 AM   PM User | #1
rupert
New Coder

 
Join Date: May 2011
Posts: 17
Thanks: 4
Thanked 0 Times in 0 Posts
rupert is an unknown quantity at this point
remove tag

Hi all, here i got a problem, i want to remove all the Li tag that with a classname"test" .
Code:
<html>
<head>
<title>Introduction to the DOM</title>
<script>
window.onload = function(){
var li = document.getElementsByTagName("li");
for ( var j = 0; j < li.length; j++ ) {
     if(li[j].getAttribute('class')=='test'){
li[j].parentNode.removeChild( li[j] );
     }
}
};
</script>
</head>
<body>
<h1>Introduction to the DOM</h1>
<p class="test">There are a number of reasons why the 
DOM is awesome, here are some:</p>
<ul>
<li class="ppp">ppp.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
</ul>
</body>
</html>
rupert is offline   Reply With Quote
Old 07-16-2011, 09:08 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Your code works in Firefox. If you are aiming IE too, use
Code:
if(li[j].className=='test')
instead.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 07-16-2011, 03:17 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Code:
<html>
<head>
<title>Introduction to the DOM</title>
<script>
window.onload = function(){
  var li = document.getElementsByTagName("li");
  for ( var j = 0; j < li.length; j++ ) {  
    if(li[j].className=='test') { li[j].style.display = 'none'; }
  }
};
</script>
<style type="text/css">
  .test { list-style-type:none; }
</style>

</head>
<body>
<h1>Introduction to the DOM</h1>
<p class="test">There are a number of reasons why the 
DOM is awesome, here are some:</p>
<ul>
<li class="ppp">ppp.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
</ul>
</body>
</html>
This seems to work in both MSIE and FF.
jmrker is offline   Reply With Quote
Old 07-17-2011, 10:55 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Code:
 
if(li[j].className=='test') { li[j].style.display = 'none'; }
that only hides the elements. It doesn't "remove" them like the op wants.

@op

using abduraooft's suggestion in your code should work in all major browsers.
bullant is offline   Reply With Quote
Old 07-18-2011, 07:58 AM   PM User | #5
rupert
New Coder

 
Join Date: May 2011
Posts: 17
Thanks: 4
Thanked 0 Times in 0 Posts
rupert is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
Code:
<html>
<head>
<title>Introduction to the DOM</title>
<script>
window.onload = function(){
  var li = document.getElementsByTagName("li");
  for ( var j = 0; j < li.length; j++ ) {  
    if(li[j].className=='test') { li[j].style.display = 'none'; }
  }
};
</script>
<style type="text/css">
  .test { list-style-type:none; }
</style>

</head>
<body>
<h1>Introduction to the DOM</h1>
<p class="test">There are a number of reasons why the 
DOM is awesome, here are some:</p>
<ul>
<li class="ppp">ppp.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
<li class="test">It's easy to use.</li>
<li class="test">It can help you to find what you want, really quickly.</li>
</ul>
</body>
</html>
This seems to work in both MSIE and FF.
yeah, it just hide them,but i want to remove all of them..
rupert is offline   Reply With Quote
Old 07-18-2011, 04:54 PM   PM User | #6
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
Quote:
Originally Posted by rupert View Post
Hi all, here i got a problem, i want to remove all the Li tag that with a classname"test" .
Grab this function: http://code.google.com/p/getelementsbyclassname

Works great in all browsers. Even works in MSIE.
__________________
"Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
Krupski is offline   Reply With Quote
Old 07-18-2011, 04:57 PM   PM User | #7
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
Quote:
Originally Posted by rupert View Post
yeah, it just hide them,but i want to remove all of them..
Here's a neat little function that removes tags but leaves the text content in place:

Code:
/***
 * remove tag specified by element e
 * text content remains
 ***/
var removeTags = function (e) {
    e.parentNode.insertBefore(document.createTextNode(e.innerHTML), e);
    return e.parentNode.removeChild(e);
}
__________________
"Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
Krupski is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 01:50 AM.


Advertisement
Log in to turn off these ads.