Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 10-28-2012, 10:44 AM   PM User | #1
Vraith
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Vraith is an unknown quantity at this point
Hide class

Hello

My assignment is to find all <p> and hide the ones that are <p class="show"> using a For Loop. "show" is supposed to be hidden when loading the page, because im gonna do a < a > that is gonna show them. This is what i have done, and it doesnt work. Did i do something wrong or am i way off here ?

windows.onload = prepareShow;
function prepareShow(){
var fan = document.getElementsByClassName("show");
for (var i = 0; i < fan.length; i++){
if (fan[i].getAttribute("class")=="show"){
fan[i].style.display="none";}
}
}
Vraith is offline   Reply With Quote
Old 10-29-2012, 12:47 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
Can be done a few ways but :
Code:
<html>
<head>
<style type="text/css">
.show{
	display: none;
}
</style>
</head>
<body>
<p class="show">bill</p>
<p class="abit">See</p>
<p class="show">dick</p>
<p class="abit">Me</p>
<p class="show">jane</p>
<p class="abit">Run</p>
<button onclick="LetsSee()">Behold</button>

<script type="text/javascript">
function LetsSee()
{
	var x = document.getElementsByClassName("show").length;
	for(i = 0; i < x; i++ ){
	var fan = document.getElementsByClassName("show")[i].style.display = 'block';
	}
}
</script>
</body>
</html>
sunfighter is offline   Reply With Quote
Old 10-29-2012, 02:16 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You can make the code a lot shorter than that:

Code:
function LetsSee()
{
	var x = document.getElementsByClassName("show");
	for(i = x.length-1; i >0 0; i-- ){
	x[i].style.display = 'block';
	}
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 10-29-2012, 06:46 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by felgall View Post
You can make the code a lot shorter than that:

Code:
function LetsSee()
{
	var x = document.getElementsByClassName("show");
	for(i = x.length-1; i >0 0; i-- ){
	x[i].style.display = 'block';
	}
}

You can make the code a lot shorter than that, (and re-usable):

Code:
function LetsSee(c,s,r){r=(r||document).getElementsByClassName(c);
 for(c in x)(x[c].style||0).display=s}

usage: LetsSee(classToHit , displayStyleToSet, opt_elmRoot );

ex (do what the previous codes do):
Code:
LetsSee("show","block");
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 10-29-2012, 07:34 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by rnd me View Post
Code:
function LetsSee(c,s,r){r=(r||document).getElementsByClassName(c);
 for(c in x)(x[c].style||0).display=s}
Shouldn't that be

Code:
function LetsSee(c,s,r){x=(r||document).getElementsByClassName(c);
 for(c in x)(x[c].style||0).display=s}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall 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 03:33 PM.


Advertisement
Log in to turn off these ads.