Thanh Nguyen
Thanh Nguyen

Reputation: 5342

Get element by href in jquery not working?

I want to add class (d_select) to a tag which href is "#tut" by click at Show.

But When i click, nothing happens. Here is my code:

 <a class="show"><span>Show!</span></a>       

            <ul class="tab_cm">
                <li><a href="#details">Details</a></li>
                <li><a href="#feed">Feedback</a></li>
                <li><a href="#tut">Tutorial</a></li>
                <li><a href="#map">Map</a></li>
            </ul>

<script>
$(function(){
    $(".show").click(function(event){
    event.preventDefault();
    $("ul.tab_cm").find('a[href="#tut"]').addClass("d_select");
    });
});
</script>

I speak English not well, i'm so sorry!

Upvotes: 1

Views: 1449

Answers (1)

Plynx
Plynx

Reputation: 11461

Try $("ul.tab_cm").find('a[href="\\#tut"]').addClass("d_select");

\\ will escape a character that has special CSS meaning when used as part of a selector... although it seems JQuery is smart enough to know the # in an attribute reference is a literal anyway, so this shouldn't be required.

Upvotes: 2

Related Questions