3

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.

2 Answers 2

6
$('.preview').has('.title:hidden').addClass('yournewclass');
Sign up to request clarification or add additional context in comments.

1 Comment

The logic here is the wrong way around - he wants to add the class if the title is NOT visible
4

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');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.