whitemonkey2
08-17-2009, 05:16 PM
:confused:This is probably a snap for some of you but my JS skills are still beginner.
Below is an example fairly common show hide div script.
Works great to hide/show a single item.
I can even use it to create multiple checkboxes to hide/show different items.
But the problem is I need to use a single checkbox to hide a specified set of multiple id's to hide a bunch of divs. Its not an option to put the div's I want to hide in a single wrapper becuase theyre in different places.
I have been told you can do this by putting all the id's into an array.
I know a little about arrays but I haven't been able to figure out how to link an array with a script like below.
it should work this way
onclick checkbox=show this array of id's if box checked
onclick checkbox=hide the same array of id's again if box unchecked
(most divs would be set to display none by default)
I have seen suggestions to give everything the same id (not a good idea)
or use get element by name or class but my requirements are to do it by id's.
If somone could modify this to show a full working example how a single checkbox can be used to hide/show array of id's that would be great.
Doesn't need to be crossbrowser just ie6+. Simple as possible is best.
Thanks in advance.
here is the example to work with single id's
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
//show hide div script
<!--
function showMe (it, box) {
var vis = (box.checked) ? "block" : "none";
document.getElementById(it).style.display = vis;
}
function showMe2 (it) {
var vis = document.getElementById(it).style.display
if (vis == "block") { document.getElementById(it).style.display = "none"; }
else { document.getElementById(it).style.display = "block"; }
}
//-->
</script>
<style type="text/css">
<!--
.divstyle
{
display: none;
border: 1px solid #000;
height: 100px;
width: 200px;
}
-->
</style>
</head>
<body>
<p>
<input type="checkbox" onclick="showMe('div1', this)" />
show div 1
<input type="checkbox" onclick="showMe('div2', this)" />
show div 2
<input type="checkbox" onclick="showMe('div3', this)" />
show div 3
<input type="checkbox" onclick="showMe('div4', this)" />
show div 4</p>
<div class="divstyle" id="div1">Content div1</div>
<div class="divstyle" id="div2">Content div2</div>
<div class="divstyle" id="div3">Content div3</div>
<div class="divstyle" id="div4">Content div4</div>
</body>
</html>
Here is a fragment of another script I found that is supposed to do something close to what i want. I was trying to merge with the other one but couldn't quite figure it out.
<script type="text/javascript">
function hide_elements(div1,div2,div3,div4){
var elems=new Array();
elems=elementids.split(',');
var len = elems.length;
for(i=0;i<len;i++){
element = document.getElementById(elems[i]);
element.style.display = 'none';
};
};
function show_elements(div1,div2,div3,div4){
var elems=new Array();
elems=elementids.split(',');
var len = elems.length;
for(i=0;i<len;i++){
element = document.getElementById(elems[i]);
element.style.display = 'block';
};
};
</script>
Below is an example fairly common show hide div script.
Works great to hide/show a single item.
I can even use it to create multiple checkboxes to hide/show different items.
But the problem is I need to use a single checkbox to hide a specified set of multiple id's to hide a bunch of divs. Its not an option to put the div's I want to hide in a single wrapper becuase theyre in different places.
I have been told you can do this by putting all the id's into an array.
I know a little about arrays but I haven't been able to figure out how to link an array with a script like below.
it should work this way
onclick checkbox=show this array of id's if box checked
onclick checkbox=hide the same array of id's again if box unchecked
(most divs would be set to display none by default)
I have seen suggestions to give everything the same id (not a good idea)
or use get element by name or class but my requirements are to do it by id's.
If somone could modify this to show a full working example how a single checkbox can be used to hide/show array of id's that would be great.
Doesn't need to be crossbrowser just ie6+. Simple as possible is best.
Thanks in advance.
here is the example to work with single id's
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
//show hide div script
<!--
function showMe (it, box) {
var vis = (box.checked) ? "block" : "none";
document.getElementById(it).style.display = vis;
}
function showMe2 (it) {
var vis = document.getElementById(it).style.display
if (vis == "block") { document.getElementById(it).style.display = "none"; }
else { document.getElementById(it).style.display = "block"; }
}
//-->
</script>
<style type="text/css">
<!--
.divstyle
{
display: none;
border: 1px solid #000;
height: 100px;
width: 200px;
}
-->
</style>
</head>
<body>
<p>
<input type="checkbox" onclick="showMe('div1', this)" />
show div 1
<input type="checkbox" onclick="showMe('div2', this)" />
show div 2
<input type="checkbox" onclick="showMe('div3', this)" />
show div 3
<input type="checkbox" onclick="showMe('div4', this)" />
show div 4</p>
<div class="divstyle" id="div1">Content div1</div>
<div class="divstyle" id="div2">Content div2</div>
<div class="divstyle" id="div3">Content div3</div>
<div class="divstyle" id="div4">Content div4</div>
</body>
</html>
Here is a fragment of another script I found that is supposed to do something close to what i want. I was trying to merge with the other one but couldn't quite figure it out.
<script type="text/javascript">
function hide_elements(div1,div2,div3,div4){
var elems=new Array();
elems=elementids.split(',');
var len = elems.length;
for(i=0;i<len;i++){
element = document.getElementById(elems[i]);
element.style.display = 'none';
};
};
function show_elements(div1,div2,div3,div4){
var elems=new Array();
elems=elementids.split(',');
var len = elems.length;
for(i=0;i<len;i++){
element = document.getElementById(elems[i]);
element.style.display = 'block';
};
};
</script>