• 0

[JavaScript] hide show an array od div's


Question

hi folks, I would be really thankfull if someone has a script that hides and show an array of div's.

I need that to show stores in some regions, and some stores are in many regions if not all regions, so i want to filter out stores that are in a chosen region.

if i select number 2 in the drop down, i would see "blah 2" and "blah 3" and so on... Can anyone help?

Here is the code:

<html>
<head>
<script type="text/javascript"> 
function ShowMeWhatIWant(chosen){ 
	if (chosen = 1) {
	// show div 1
	// hide div 2 and 3
	}
	elseif (chosen = 2) {
	//show div 2 and 3
	// hide div 1
	}
	elseif (chosen = 3) {
	//show div 1 and 3
	// hide div 2
	}
	else {
	//show all div's
	}
} 
</script> 
</head>
<body>


<select onChange="java script:ShowMeWhatIWant(this)"> 
	<option value="1">1</option> 
	<option value="2">2</option> 
	<option value="3">3</option> 
</select> 


<div id="first">blah</div>
<div id="second">blah</div>
<div id="third">blah</div>


</body>
</html>

Edited by WAR-DOG
Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0
<script type="text/javascript">
function ShowMeWhatIWant(chosen){
	var first = document.getElementById('first');
	var second = document.getElementById('second');
	var third = document.getElementById('third');

	first.style.display = 'block';
	second.style.display = 'block';
	third.style.display = 'block';
	if (chosen == 1) {
		second.style.display = 'none';
		third.style.display = 'none';
	}elseif(chosen == 2){
		first.style.display = 'none';
	}elseif(chosen == 3){
		second.style.display = 'none';
	}
}
</script>

Should help.

Link to comment
Share on other sites

  • 0

A! nice... but it doesn't work when i try it. I also modified it a little :

<script type="text/javascript"> 
function ShowMeWhatIWant(chosen){
	var first = document.getElementById('first');
	var second = document.getElementById('second');
	var third = document.getElementById('third');

	if (chosen == 1) {
		first.style.visibility = 'visible'
		second.style.visibility = 'hidden';
		third.style.visibility = 'hidden';
	} else if (chosen == 2){
		first.style.visibility = 'hidden';
		second.style.visibility  = 'visible'
		third.style.visibility  = 'visible'
	} else if (chosen == 3){
		first.style.visibility  = 'visible'
		second.style.visibility = 'hidden';
		third.style.visibility  = 'visible'
	} else {
		first.style.visibility  = 'visible'
		second.style.visibility  = 'visible'
		third.style.visibility  = 'visible'
	}
}
</script>

Link to comment
Share on other sites

  • 0

Add alert(chosen); to the top of the function and see what the value is. If it matches what you have in the conditionals, then you likely need to execute parseInt() on chosen or put 1, 2, and 3 in quotes since, IIRC, it's being treated as a string, not an integer.

Link to comment
Share on other sites

  • 0

The alert() did really help me figure out my problem. So I took the time to search google and learned about javascript+select :)

So here is my solution:

<html>
<head>
<script type="text/javascript"> 
function ShowMeWhatIWant(){
	var chosen = document.TheForm.TheList.options[document.TheForm.TheList.selectedIndex].value
	var first = document.getElementById('first');
	var second = document.getElementById('second');
	var third = document.getElementById('third');
	if (chosen == '1') {
		first.style.display = 'block'
		second.style.display = 'none'
		third.style.display = 'none'
	} else if (chosen == '2'){
		first.style.display = 'none'
		second.style.display = 'block'
		third.style.display = 'block'
	} else if (chosen == '3'){
		first.style.display = 'block'
		second.style.display = 'none'
		third.style.display = 'block'
	} else {
		first.style.display = 'block'
		second.style.display = 'block'
		third.style.display = 'block'
	}
}
</script>
</head>
<body>

<form name="TheForm">
<select name="TheList" onChange="java script:ShowMeWhatIWant()"> 
	<option>none</option> 
	<option value="1">1</option> 
	<option value="2">2</option> 
	<option value="3">3</option> 
</select> 
</form>

<div id="first">blah 1</div>
<div id="second">blah 2</div>
<div id="third">blah 3</div>


</body>
</html>

Thank you all for helping!

Link to comment
Share on other sites

  • 0

I found a flaw in the code, it only changes the first div's with the same name, because ID's are intended to be unique, unfortunatly there is no getElementByClass...

Link to comment
Share on other sites

  • 0
I found a flaw in the code, it only changes the first div's with the same name, because ID's are intended to be unique, unfortunatly there is no getElementByClass...

http://mootools.net/ is much smaller then jquery and much more flashy and responsive if you ask me...

also you could just do somthing easy like this....

function show(){

var MYobjects =["divID1","divIDa","divIDsomthing"];

for(item in MYobjects){

document.getElementById(item).styles.visibility = visible.

}

}

In moo tools you could even reduce it further to somthing like this

function show(){

$('MainDiv').getElements('div[id$=toggleDIV]').each(function(div){

//get all div tags with id ending with 'toggleDIV' inside the Div MainDiv

div.setStyle('visibility', 'visible');

//Sets them all to visible

});

}

function hide(){

$('MainDiv').getElements('div[id$=toggleDIV]').each(function(div){

//get all div tags with id ending with 'toggleDIV' inside the Div MainDiv

div.setStyle('visibility', 'none');

//Sets them all to invisible

});

}

EDIT:::

for a real quick loop if you wanted all items with a certain style you could use $$ in mootools eg

$$('toggleDiv').each(function(div){

//get all elements with css selector = 'toggleDIV' in the DOM

div.setStyle('visibility', 'none');

//Sets them all to invisible

});

Edited by 72001
Link to comment
Share on other sites

  • 0

Ok, i have a small solution for the problem :)

<script type="text/javascript">
function ShowMe(picked){

	var area = document.getElementById("areaofeffect");
	var divs = area.getElementsByTagName("div");
	var goes = divs.length;

	if(picked=='0'){for(i=0; i<goes; i++){divs[i].style.display="block";} return false}
	for(i=0; i<goes; i++){
		if(divs[i].className!="p"+picked){
			divs[i].style.display="none";
		}
		else{
			divs[i].style.display="block";
		}
	}

} 

</script>
</head>
<body>

<form>
<label>Choose Region</label>
<select onChange="ShowMe(this.value)"> 
	<option value="0" selected>none</option> 
	<option value="1">Region 1</option> 
	<option value="2">Region 2</option> 
	<option value="3">Region 3</option> 
	<option value="4">Region 4</option> 
</select> 
</form>

<div class="test">control subject</div>
<div id="tester">control subject1</div>

<div id="areaofeffect">
	<div class="p1">Partner 1 Region 1</div>
	<div class="p2">Partner 2 Region 2</div>
	<div class="p1">Partner 3 Region 1</div>
	<div class="p3">Partner 4 Region 3</div>
	<div class="p4">Partner 5 Region 4</div>
	<div class="p1">Partner 6 Region 1</div>
	<div class="p2">Partner 7 Region 2</div>
	<div class="p3">Partner 8 Region 3</div>
</div>

</body>
</html>

Link to comment
Share on other sites

  • 0

thanks for the code war-dog.

Was after something like this for part of my personal site, being trying for days to work something out and your solution works perfectly :D

Link to comment
Share on other sites

  • 0

Since one partner can be represented in many regions, if not all I improved the code:

<script type="text/javascript">
function trim(str, charlist) {
	charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
	var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
	return str.replace(re, '');
}

function explode( delimiter, string ) {
	var emptyArray = { 0: '' };
	if ( arguments.length != 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined' ) {
		return null;
	}

	if ( delimiter === '' || delimiter === false || delimiter === null ) {
		return false;
	}

	if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' ) {
		return emptyArray;
	}

	if ( delimiter === true ) {
		delimiter = '1';
	}

	return string.toString().split ( delimiter.toString() );
}

function ShowMeWhatINeed(picked){

	var area = document.getElementById("areaofeffect");
	var divs = area.getElementsByTagName("div");
	var goes = divs.length;
	var tmp  = [];
	var ingoes;
	var chk;

	if(picked=='0') { for(i=0; i<goes; i++) { divs[i].style.display="block";} return false }

	for(i=0; i<goes; i++) {
		tmp=explode(" ", divs[i].className);
		ingoes=tmp.length;
		for(j=0; j<ingoes; j++){
			if(trim(tmp[j])!="p"+picked) {
				divs[i].style.display="none";
			}
			else {
				divs[i].style.display="block";
				break;
			}
		}
	}
}
</script>
</head>
<body>

<form>
<label>Choose Region</label>
<select onChange="ShowMeWhatINeed(this.value)"> 
	<option value="0" selected>none</option> 
	<option value="1">Region 1</option> 
	<option value="2">Region 2</option> 
	<option value="3">Region 3</option> 
	<option value="4">Region 4</option> 
</select> 
</form>

<div class="test">control subject</div>
<div id="tester">control subject1</div>

<div id="areaofeffect">
	<div class="p1 p2">Partner 1 Region 1,2</div>
	<div class="p2">Partner 2 Region 2</div>
	<div class="p1 p3">Partner 3 Region 1,3</div>
	<div class="p3">Partner 4 Region 3</div>
	<div class="p4">Partner 5 Region 4</div>
	<div class="p1">Partner 6 Region 1</div>
	<div class="p2">Partner 7 Region 2</div>
	<div class="p3">Partner 8 Region 3</div>
</div>

</body>
</html>

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.