Marco
Marco

Reputation: 2737

How to add a class name to a specific selector

Considering the following html:

<div class="box">
    <ul class="tabs">
        <li><a href="publication#all">ALL</a></li>
        <li><a href="publication#country">COUNTRY</a></li>
        <li><a href="publication#date">DATE</a></li>
    </ul>

    <div id="tab_all">...</div>

    <div id="tab_country">...</div>
    <div id="tab_date">...</div>
</div>

From a specific element, i want to add a class to the anchor a with an href of "publication#country"

This is what i've done so far with no success.

$("#tab_date").siblings("ul.tabs").find('li a[href=publication#country').addClass("current");

Upvotes: 0

Views: 56

Answers (2)

Paul Richter
Paul Richter

Reputation: 11072

You forgot the closing bracket. Try this:

$("#tab_date").siblings("ul.tabs").find("li a[href='publication#country']").addClass("current");

Note: I originally thought the attribute selector value needed to be quoted (so I normally write it as above), however according to Rhumborl, and confirmed by Scott and myself, it does appear to work without the quoting. However it would be safer to get in the habit of including the quotes in case you encounter an unusual situation where omitting the quotes would cause issues.

Upvotes: 3

Scott
Scott

Reputation: 3765

You can select it via:

$("#tab_date").siblings("ul.tabs").find("li a[href=publication#country]");

Working fiddle: http://jsfiddle.net/mfx0c93t/6/

Upvotes: 3

Related Questions