Googly
Googly

Reputation: 3

My signs aren't changing

I am new to JS just playing around to understand how it works.

Why isn't my sign (+,-) changing? When the div expand it remains with a + sigh never goes to -

Thanks

$(document).ready(function(){
    $(".expanderHead").click(function(){
        $(this).next(".expanderContent").slideToggle();
        if ($(".expanderSign").text() == "+"){
            $(".expanderSign").html("−")
        }
        else {
            $(".expanderSign").text("+")
        }
    });
});

Upvotes: 0

Views: 93

Answers (1)

Barmar
Barmar

Reputation: 781751

Just guessing at the relationship, since you haven't shown your HTML, but you probably need something like this:

$(document).ready(function () {
    $(".expanderHead:visible").click(function () {
        var content = $(this).next(".expanderContent");
        var sign = $(this).find(".expanderSign");
        if (content.is(":visible")) {
            content.slideUp();
            sign.text("+");
        } else {
            var expanded = $(".expanderContent:visible");
            if (expanded.length > 0) {
                expanded.slideUp();
                expanded.prev(".expanderHead").find(".expanderSign").text("+");
            }
            content.slideDown();
            sign.text("-");
        }
    });
});

FIDDLE

Upvotes: 1

Related Questions