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 12-19-2012, 03:15 PM   PM User | #1
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
hide all divs except those with checked boxes?

hello, I'm wondering how I can hide all divs on a page that do not have a checkbox thats checked.

my html;

PHP Code:
<div id='2_ImageBlock' style='width: 170px; height: 170px; border: 1px solid grey; float: left; overflow:hidden;    padding: 3px; margin: 4px;'>
    <
label><input type='checkbox' name='SelectedImage[]' id='SelectedImage' value='Image.jpg' /> Use Image<br />
    <
img src='Image.jpg' width='150'  />
    </
label>
</
div
using jquery, maybe something like this?

$('div:' + $("input:checkbox").not(":checked")).hide();
angst is offline   Reply With Quote
Old 12-19-2012, 03:22 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
<div id='2_ImageBlock'

An id may not start with a digit

Do you mean that you want to show all the divs where the associated checkbox is checked and hide those where the checkbox is unchecked?
Obviously the checkboxes must remain visible.


Code:
<input type="checkbox" id = "cbox1" onclick="showMe('div1', this)" checked>
<div id="div1" style="display:block;">XYZ</div>

<script type = "text/javascript">

function showMe (whichdiv, box) {
var vis = (box.checked) ? "block" : "none";
document.getElementById(whichdiv).style.display = vis;
}

</script>
It is your responsibility to die() if necessary….. - PHP Manual
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-19-2012 at 03:56 PM..
Philip M is offline   Reply With Quote
Old 12-19-2012, 03:43 PM   PM User | #3
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
yes, thats been changed, but it doesn't solve anything...
angst is offline   Reply With Quote
Old 12-19-2012, 03:44 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if your html will always have that structure...
Code:
<script>
var divs=document.getElementsByTagName("div")
  for (var i = 0; i < divs.length; i++) {
divs[i].style.display=divs[i].children[0].children[0].checked?"block":"none";
}
</script>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
angst (12-19-2012)
Old 12-19-2012, 03:58 PM   PM User | #5
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
thats perfect, one last question. how can I exclude another static div?


thanks again!
angst is offline   Reply With Quote
Old 12-19-2012, 04:00 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
give the divs you want to operate on a class name (that way you can get rid of that ugly inline styling, too)...

Code:
<html>
<head>
<style>
.imgcont{
width: 170px; 
height: 170px; 
border: 1px solid grey; 
float: left; 
overflow:hidden;    
padding: 3px; 
margin: 4px;
}
</style>
</head>
<body>
<input type="button" value="show 'em" onclick="showHide()"/><br>
<div id='ImageBlock2' class='imgcont'> 
    <label><input type='checkbox' name='SelectedImage[]' id='SelectedImage' value='Image.jpg'/> Use Image<br /> 
    <img src='Image.jpg' width='150'  /> 
    </label> 
</div>
<div id='ImageBlock3' class='imgcont'> 
    <label><input type='checkbox' name='SelectedImage[]' id='SelectedImage' value='Image.jpg' /> Use Image<br /> 
    <img src='Image.jpg' width='150'  /> 
    </label> 
</div>
<script>
function showHide(){
var divs=document.getElementsByTagName("div")
  for (var i = 0; i < divs.length; i++) {
  if(divs[i].className=="imgcont"){
divs[i].style.display=divs[i].children[0].children[0].checked?"block":"none";
		}
	}
}
</script>
</body>
</html>
xelawho 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 10:24 AM.


Advertisement
Log in to turn off these ads.