Chris P
Chris P

Reputation: 2357

css jquery changing class style

$("#menu li").click(function () {
        $("#menu .active").removeAttr("class");
        $(this).attr("class","active");
    });

        <div id="menu">
            <ul>
                <li id="m1" class="active"><a>link 1</a></li>
                <li id="m2"><a>link 1</a></li>
                <li id="m3"><a>link 2</a></li>
                <li id="m4"><a>link 3</a></li>
                <li id="m5"><a>link 4</a></li>
                <li id="m6"><a>link 5</a></li>
                <li id="m6"><a href="">link 6</a></li>
                <li id="m7"><a href="">link 7</a></li>
                <li id="m8"><a>link 8</a></li>
            </ul>
        </div>

css:

#menu .active{
    background:white;
}

#menu .active a{
    opacity:0.5;
}

#menu a{
    color:#08042b;
    text-decoration:none;
    font-size:14px;
    text-shadow: 1px 0px 1px #6055c8;
    position:relative;

}

When i click a link the first time everything seems to be ok.. When i click for second time background-color of this li changed li font takes opacity 0.5, previous li hasn't active className but previous li text hasn't go to opacity:1 Any help?

Upvotes: 0

Views: 695

Answers (1)

Rafael Steil
Rafael Steil

Reputation: 4697

As it was said, using $("#menu .active").removeClass("active"); is more correct than removing the entire class attribute was you did, as you may ending losing other classes you may have, thus resulting in unexpected behavior.

Upvotes: 2

Related Questions