0

So this is my current jQuery code:

$('.aProduct .inner').each(function() {
    var length = $.trim($(this).text()).length;    
    if(length > 20){
        $(this).css("max-height", "20px");
    }
    else {
        $(this).css("background", "blue");
    }
});

And it says: if the length of the characters is bigger than 20, apply this CSS to .aProduct .inner:

$(this).css("max-height", "20px");

However, is it possible to give this max-height to the parent of this class? I don't mean just to replace this by .aProduct .omschrijving, because then ALL the .aProduct .omschrijving would have a max-height of 20. I only want the parent the parent of which the class .aProduct .omschrijving has more characters than 20, to have a max-height of 20px.

HTML:

<div class="aProductHeader">
    <div class="omschrijving">
        <h3>omschrijving</h3>
    </div>
</div>
<div class="aProduct">
    <div class="omschrijving">
        <span class="entry inner">
            Toddler bestek blauw
        </span>
    </div>
</div>

This probably sounds really confusing, but I don't know how else to explain this.

1

1 Answer 1

3

Change:

if(length > 20){
    $(this).css("max-height", "20px");
}

to:

if(length > 20){
    $(this).closest('.omschrijving').css("max-height", "20px");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Works beautifully. Is there also a solution in case I would also like to style the .omschrijving class from the different parent .aProductHeader?
Based on the same criteria of the text length?
Yes, in the same function.
$(this).closest('.aProduct').prev('.aProductHeader').find('.omschrijving').css("max-height", "20px"); will select the <div class="omschrijving"> from the previous <div class="aProductHeader">.
Wow, this looks a bit more complicated than I thought. I'll have to look this up a bit more. Thank you for the help!

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.