2

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

3 Answers 3

8

Set the hash property:

window.location.hash = $(this).attr('id');
Sign up to request clarification or add additional context in comments.

Comments

3

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
});

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.