kalaba2003
kalaba2003

Reputation: 1229

Why do <a href=#A> and jquery click function not work together?

What I want is that: There is a link on top of page. When it is clicked, it should go to in the same page and toggle a div class with jquery.But both of are not working at the same time.. For example:

<a href='#A' id='link'>Go to A and toggle a div class</a>

jquery codes are:

$('#link').click(function () {
    $('.toggle').toggle();
   });

   <a name='A'> Here is A </a>

my codes are more complex than that but same logic with I write here. When I click the link, the div which has toggle class toggles. First it looks jquery codes. But it does not go to #A. That is to say, href attribute is not working. Any idea?...

Upvotes: 1

Views: 3112

Answers (1)

strah
strah

Reputation: 6722

click() will not fire the default event so you have to set window.location

$('#link').click(function (e) {
    e.preventDefault();
    $('.toggle').toggle();
    window.location.hash = ($(e.currentTarget).attr("href"));
});

Upvotes: 3

Related Questions