Setting CSS attributes on Change using jQuery
Posted
by
Nick B
on Stack Overflow
See other posts from Stack Overflow
or by Nick B
Published on 2014-06-03T21:10:19Z
Indexed on
2014/06/03
21:24 UTC
Read the original article
Hit count: 150
I want to change css visibility
and display
attributes using jQuery on click
when the state of another div
's visibility
attribute changes. (Many apologies for the obfuscated markup, but needing to manipulate someone else's construction):
There are four instances of [data-label="Checkbox"] [data-label="Checked"]
in this page. I want to set [data-label="trash"]
and [data-label="Sort Options"]
to visibility: visible; display: [empty value]
when any of the [data-label="Checkbox"] [data-label="Checked"]
's attributes changes to 'visibility', 'visible'
.
Else, if none of [data-label="Checkbox"] [data-label="Checked"]
's have the attribute 'visibility', 'visible'
, I want to set [data-label="trash"]
and [data-label="Sort Options"]
back to their initial states: display: none; visibility: hidden;
.
Here's the markup:
<div data-label="Sort Options" style="display: none; visibility: hidden;">
<div data-label="trash" style="display: none; visibility: hidden;"></div>
</div>
<div data-label="Checkbox">
<div data-label="Unchecked"></div>
<div data-label="Checked" style="display: none; visibility: hidden;"></div>
</div>
Here is what I have tried unsuccessfully:
$('[data-label="Checkbox"]').click(function() {
if ('[data-label="Checkbox"] [data-label="Checked"]').css('visibility', 'visible') {
$('[data-label="trash"], [data-label="Sort Options"]').css({'display': '', 'visibility': 'visible'});
} else {
$('[data-label="trash"], [data-label="Sort Options"]').css({'display': 'none', 'visibility': 'hidden'});
}
});
Any help would be greatly appreciated! Thanks
© Stack Overflow or respective owner