0

I have a HTML code like this,

<div class="panel-footer">
    <div class="post-activity">
        <i class="loading-icon"></i>
        <button onclick="ChangeColor(this);">Change Color</button>
    </div>
    <div class="comments-ch"></div>
</div>

When I write this Jquery code

function ChangeColor(element)
{
    $(element).closest(".panel-footer").find(".comments-ch").css("background-color","#CC0000")
}

Not working for class = comments-ch, But If I write this code like this,

    function ChangeColor(element)
    {
        $(element).closest(".panel-footer").find(".post-activity").css("background-color","#CC0000")
    }

working.

Summary, first div under the "panel-footer" is OK, but the second/last div NOT OK.

How can i reach the second/last div element? Thanks

7
  • 1
    Works for me jsfiddle.net/j08691/wwxw2wok. Have you checked the browser's console for errors? Commented Aug 31, 2014 at 19:00
  • It should definitely be working. Perhaps something is wrong with the HTML structure? clas= or some other typo? Commented Aug 31, 2014 at 19:04
  • 1
    Does the <div class="comments-ch"></div> have any contents or styles to give it dimensions? Being empty, it can be 0px tall, allowing the script to succeed but the effects may just not be easily seen. Commented Aug 31, 2014 at 19:05
  • Yeah, this div have a also style="display:none" code. Commented Aug 31, 2014 at 19:06
  • 1
    If it's display:none, you'll need to .show() it after setting the css. or slideDown, or fadeIn or whatever. Commented Aug 31, 2014 at 19:08

2 Answers 2

1

Try .show() after setting the CSS:

function ChangeColor(element) {
    $(element).closest(".panel-footer").find(".comments-ch").css("background-color", "#CC0000").show()
}
Sign up to request clarification or add additional context in comments.

Comments

1

When using a class selector, make sure you specify a period in front.

For example, in:

$(element).closest(".panel-footer").find("comments-ch").css("background-color","#CC0000")

Change from find("comments-ch") to find(".comments-ch")

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.