tags:

views:

40

answers:

3

I'd like if I could click a link with class "query" and have it's id attribute come after the hash. For example, a link that looks like this:

<a href="#" id="members" class="query">Members</a>

When clicked would change the url from example.com/users to example.com/users#members.

Here's my code so far:

$('.query').click(function(event){
    event.preventDefault();
    window.location.href = $(this).attr('id');
});

Right now clicking the link just moves the url to example.com/members

+4  A: 

Set the hash property:

window.location.hash = $(this).attr('id');
SLaks
+1  A: 

Try window.location.hash instead of window.location.href

Nazariy
+2  A: 

Isn't it silly to have the id attribute come from the hash? Why not just ... Is it unreasonable to do this? Anyway, you can do this:

$(".query").click(function () {
   window.location.hash = this.id;
   return false; // prevent default link follow
});
tandu