user219932
user219932

Reputation:

Selecting a href attribute of a link using jQuery?

I have the following code that is not working properly, what am I doing wrong???

$('#gallerynav ul li a').click(function(){
    var href = $('#gallerynav ul li a').attr('href')
    $("#galleries").children().fadeOut(500 , function(){
        (href).fadeIn(500)
    })
})

I have the href of the links set like this:

<div id="gallerynav">
    <ul>
        <li><a href="#foo">link</a></li>
    </ul> 
</div>

Upvotes: 0

Views: 10585

Answers (4)

Try this.

$('#gallerynav ul li a').click(function(){        
    var link = $(this).attr('href'); 
    $("#galleries").children().fadeOut(500 , function(){
        $(link).fadeIn(500);
    });
});

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

Try this:

$('#gallerynav ul li a').click(function(evt) {
    var href = this.href;
    $("#galleries").children().fadeOut(500 , function() {
        $(href).fadeIn(500);
    });
    evt.preventDefault();
});

Upvotes: 1

abysslogic
abysslogic

Reputation: 321

I think: var href = $('#gallerynav ul li a').attr('href');

should be: var href = this.attr('href');

Upvotes: 0

wows
wows

Reputation: 11127

It looks like you're missing a $ in front of the (href) call. Try:

$(href).fadeIn(500)

Upvotes: 0

Related Questions