0

I have looked at what seems to be a million examples of how to get the link a user clicks to change class, while ensuring the sibling links take their original class. I am very new to jQuery so admittedly I am lost when I try these different examples. Here is my css:

/*active link */
.current {
color: #f48239;
text-decoration: none;
}
/* inactive link*/
.link {
    color: black;
    text-decoration: none;

}

jQuery Function that only shows the div content that corresponds to the link being clicked:

 $(document).ready(function(){
    $('a').click(function () {
    var divname= this.name;
    $("#"+divname).show("fast").siblings().hide("fast");

    });

    });

And now the HTML:

        <div id="left">
            <UL><h2>DBACO Projects</h2></UL>
                    <div id="headerspace">
                    </div>              
            <UL ID="links"><LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="QualcommInfo">QUALCOMM BUILDING M PROJECTS</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="StFrancisInfo">ST FRANCIS OF ASSISI CATHOLIC &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMMUNITY-PHASE 1</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="YumaInfo">YUMA PIVOT POINT HOTEL AND &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONFERENCE CENTER</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="RealtyInfo">REALTY INCOME CORPORATE &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HEADQUARTERS</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="CenterCityInfo">CENTER CITY MARRIOTT</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="PalmInfo">PALM MOUNTAIN RESORT</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="TrailsInfo">THE TRAILS AT PALM SPRINGS</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="OtayInfo">OTAY LOGISTICS CENTER</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="CabrilloInfo">CABRILLO MEDICAL OFFICE BUILDING</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="NorthgateInfo">NORTHGATE COMMUNITY CHURCH</A></h6></LI><BR>
            <LI><h6 style="display: inline;"><A class="link" HREF="#" NAME="LomaInfo">LOMA ALTA VILLAGE MOB</A></h6></LI></UL>
        </div>

Any ideas on how to get the link clicked to take the class of "current" while ensuring all the other links stay with the class of "link" would be greatly appreciated.

1
  • I don't understand. Are you trying to hide and show content or change the CSS class name on the objects so that different CSS will have effect? Commented Apr 20, 2012 at 1:44

4 Answers 4

4
$(document).ready(function() {
    var $links = $('a');
    $links.click(function () {
        $links.removeClass('current').addClass('link');
        $(this).removeClass('link').addClass('current');

        var divname= this.name;
        $("#"+divname).show("fast").siblings().hide("fast");
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for everyone for your replies. MДΓΓ БДLL, your answer worked perfectly. The hiding of the DIV was working, but the changing of the class was not. Thanks again for all of your replies and help!!!
2

I am not sure why you apply "show" and "hide", but this might be what you want.

$(document).ready(function(){
    $('a').click(function () {
        $(this)
            .removeClass("link")
            .addClass("current")
            .siblings("a").removeClass("current");
    });
});

The code only does changing the css class.

Hope it helps.

1 Comment

Soe Moe, the show/hide is to show only one div of content on the right side of the page (which I did not show the code for). That was working fine, but I was needing to change the class of the one that was active at the time, and since it was a link to the same page the normal active did not seem to work. It looks like yours would have worked as well. Thank you for your reply!
1

I would suggest doing it this way: http://jsfiddle.net/3SR7E/

css:

.link.current {
color: #f48239;

}
.link {
color: black;
text-decoration: none;

}

javascript:

$(document).ready(function(){
$('a').click(function () {
var divname= this.name;
$("#"+divname).show("fast").siblings().hide("fast");
$("#links li a").removeClass("current");               
    $(this).addClass("current");

});

});

Comments

-1

here you go http://jsfiddle.net/gHWWX/4/

$(document).ready(function(){
    $('a').click(function () {
        $('a').removeClass('current').addClass('link');
        $(this).removeClass('link').addClass('current');
        var divname= this.name;
        $("#"+divname).show("fast").siblings().hide("fast");
    });
});​

minor edit

4 Comments

This is rather abysmal, style-wise.
You did the same thing, you just have the add and remove class functions on the same line, and you made a collection out of the 'a' tags.
And, both of those changes make it a LOT more efficient. You were rebuilding a list of all links in the page twice on every click. He was using a prebuilt list. That's a meaningful difference. The advantage of chaining is that you don't re-evaluate the selector over and over.
I understand the idea of chaining, it was an example of code, and it was easier to read that way. But you are absolutely correct about the collection, I totally missed that. I'll remember that next time.

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.