Body
Body

Reputation: 3688

Check for a CSS class has a property - jQuery

I have these elements;

<div class="preview">
    <div class="title" style="display:block">
</div>

<div class="preview">
    <div class="title" style="display:none">
</div>

<div class="preview">
    <div class="title" style="display:none">
</div>

I want to add a class to the preview, if the title is 'display:none'.

Please help me short this.

Thanks & Regards.

Upvotes: 3

Views: 1953

Answers (2)

Engineer
Engineer

Reputation: 48793

$('.preview').has('.title:hidden').addClass('yournewclass');

Upvotes: 6

Rory McCrossan
Rory McCrossan

Reputation: 337560

Try this:

$('.title').is(':not(:visible)').closest('.preview').addClass('foo');

Note the :not(:visible) will catch any .title elements who are hidden due to the visibility or display properties. If you only want to catch display: none try this:

$('.title').each(function() {
    if ($(this).css('display') == 'none') {
        $(this).closest('.preview').addClass('foo');
    }
});

Upvotes: 4

Related Questions