I’m wanting to hide certain divs within a container. If the sub div DOESN’T have a checkbox which is selected in it, then it should be hidden when you click the link.
eg:
if #2 and #5 checbox only where selected then divs(sub_1,sub_3,sub_4) would be hidden when link was clicked.
But if I could reference the parent via its ID or class it would be neater, as in the future this way if another parent element is introduced the javascript will not function as it should.
Thanks Paul, that hides the unchecked checkboxes very well but I need to hide their individual containing DIVs - which are about 5 parents above, or have an ID with the same number:
select_1’s containing DIV is sub_1 etc.
To separate your target checkboxes from the rest, you would have to give them a class that would permit you to target them. I have modified the previously posted code
//get all the input fields
var myCheckbox = document.getElementsByTagName(‘input’);
//process each input field
var z = 0; var zMax = myCheckbox.length;
while(z < zMax) {
if(myCheckbox[z].getAttribute(‘type’) == ‘checkbox’ && myCheckbox[z].className == ‘myClass’ && myCheckbox[z].checked == true) {
myCheckbox[z].parentNode.style.display = ‘none’;
}
z++;
}
if jQuery is what you really want to use, you would still have to give your target checkboxes a class that would permit you to target them:
$(‘.myClass:checked’).parent().css(‘display’, ‘none’);
The checkboxes have an ID, like select_1, select_2 etc.
Yeah would like to do the whole thing with jquery if possible,.
And it is the checkbox that ISN’T checked that the containing DIV (which isn’t actually a direct parent, more like a grand grand grand parent) needs to become hidden. (the containing div has the same number in ID, eg sub_1, sub 2)
You could do something like this:
//get all the input fields
var myCheckbox = document.getElementsByTagName(‘input’);
//process each input field
var z = 0; var zMax = myCheckbox.length;
while(z < zMax) {
if(myCheckbox[z].getAttribute(‘type’) == ‘checkbox’ && myCheckbox[z].checked == true) {
myCheckbox[z].parentNode.style.display = ‘none’;
}
z++;
}
Thanks, I can hack that to work for what I need.
But is there a jquery or neater way, as my webpage may have other, unlrelated checkboxes that shouldn’t be looped thru?
again thx mate