JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 12:11 PM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 55 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Can anyone help me out with simple show/hide code, please?

I'm trying to show lists of product that falls under certain categories. Categories needs to be chosen by user using select tag.

And I can't tell what's wrong with this code below.
Please, someone help me out..




Code:

<script>
	function showCertainCategory(selection)
	{ 
		var ulTag = document.getElementByTagName('ul');
               ulTag.style.display = "none";
		for( i=0; i<ulTag.length; i++)
		{
			if(ulTag.className == selection)
			{
				ulTag.style.display = "block";
			}
			else
			{
				ulTag.style.display = "none";
			}
		}
	}
	
</script>
<form>
	<select id="category" onChange="showCertainCategory(this.value)">
		<option value="handle">handle</option>
		<option value="wheel">wheel</option>
	</select>
</form>

<ul class='handle'>
	<li>Handle product A</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B</li>
	<li>wheel</li>
</ul>
<ul class='handle'>
	<li>Handle product A2</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B2</li>
	<li>wheel</li>
</ul>




Reply With Quote
  #2  
Old February 12th, 2013, 12:44 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,855 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 16 h 1 m 2 sec
Reputation Power: 813
Hi,

Quote:
Originally Posted by Ihatephp
And I can't tell what's wrong with this code below.


We can't either, because we don't know what you expect and what you get. When you get an error, please describe it so that we actually know what this is about. The JavaScript console of your browser will display the exact error message (in case there is one).

In general, you somehow confuse arrays and single elements. It's getElementsByTagName with an "s", and this method actually yields an array of HTML elements. So the loop makes sense, but the ulTag.className and ulTag.style don't.

If you rename "ulTag" to "ulTags", it probably becomes more clear.

Reply With Quote
  #3  
Old February 14th, 2013, 01:44 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 55 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Thank you

I just wanted to selected tags to be shown after onChange was triggered.

Thank you








Quote:
Originally Posted by Jacques1
Hi,



We can't either, because we don't know what you expect and what you get. When you get an error, please describe it so that we actually know what this is about. The JavaScript console of your browser will display the exact error message (in case there is one).

In general, you somehow confuse arrays and single elements. It's getElementsByTagName with an "s", and this method actually yields an array of HTML elements. So the loop makes sense, but the ulTag.className and ulTag.style don't.

If you rename "ulTag" to "ulTags", it probably becomes more clear.

Reply With Quote
  #4  
Old February 14th, 2013, 09:53 AM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 601 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 25 m 38 sec
Reputation Power: 69
I think your trying to do this (correct?):

Code:
<style>
.handle, .wheel {
display:none;
}
</style>

<script>
	function showCertainCategory(selection)
	{ 
		var ulTag = document.getElementsByTagName('ul');
		for(i=0;i<ulTag.length;i++)
		{
			if(ulTag[i].className == selection)
			{
				ulTag[i].style.display = "block";
			}
			else
			{
				ulTag[i].style.display = "none";
			}
		}
	}
	
</script>
<form>
	<select id="category" onChange="showCertainCategory(this.value)">
		<option selected>Select A Category</option>
		<option value="handle">handle</option>
		<option value="wheel">wheel</option>
	</select>
</form>

<ul class='handle'>
	<li>Handle product A</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B</li>
	<li>wheel</li>
</ul>
<ul class='handle'>
	<li>Handle product A2</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B2</li>
	<li>wheel</li>
</ul>


If not, please elaborate; on what your trying to do.

Reply With Quote
  #5  
Old February 18th, 2013, 03:53 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 55 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 32 m 31 sec
Reputation Power: 1
Thanks

Well, sort of..

But I want the contents to be full visible at the beginning and once I make change on select box, only the selected ones are going to be shown.

Does that make sense??




Quote:
Originally Posted by web_loone08
I think your trying to do this (correct?):

Code:
<style>
.handle, .wheel {
display:none;
}
</style>

<script>
	function showCertainCategory(selection)
	{ 
		var ulTag = document.getElementsByTagName('ul');
		for(i=0;i<ulTag.length;i++)
		{
			if(ulTag[i].className == selection)
			{
				ulTag[i].style.display = "block";
			}
			else
			{
				ulTag[i].style.display = "none";
			}
		}
	}
	
</script>
<form>
	<select id="category" onChange="showCertainCategory(this.value)">
		<option selected>Select A Category</option>
		<option value="handle">handle</option>
		<option value="wheel">wheel</option>
	</select>
</form>

<ul class='handle'>
	<li>Handle product A</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B</li>
	<li>wheel</li>
</ul>
<ul class='handle'>
	<li>Handle product A2</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B2</li>
	<li>wheel</li>
</ul>


If not, please elaborate; on what your trying to do.

Reply With Quote
  #6  
Old February 18th, 2013, 09:05 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 601 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 25 m 38 sec
Reputation Power: 69
try this, then:

Code:
<style>
.handle, .wheel {
display:block;
}
</style>

<script>
	function showCertainCategory(selection)
	{ 
		var ulTag = document.getElementsByTagName('ul');
		for(i=0;i<ulTag.length;i++)
		{
			if(ulTag[i].className == selection)
			{
				ulTag[i].style.display = "block";
			}
			else if(selection == "startOver")
			{
				ulTag[i].style.display = "block";
			}
			else
			{
				ulTag[i].style.display = "none";
			}
		}
	}
	
</script>
<form>
	<select id="category" onChange="showCertainCategory(this.value)">
		<option value="startOver" selected>Select A Category</option>
		<option value="handle">handle</option>
		<option value="wheel">wheel</option>
	</select>
</form>

<ul class='handle'>
	<li>Handle product A</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B</li>
	<li>wheel</li>
</ul>
<ul class='handle'>
	<li>Handle product A2</li>
	<li>handle</li>
</ul>
<ul class='wheel'>
	<li>Wheel product B2</li>
	<li>wheel</li>
</ul>

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Can anyone help me out with simple show/hide code, please?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap