user1681891
user1681891

Reputation: 281

Change Content With Anchor

Is there any easy way to have an anchor change page content when clicked? Would my best bet be to set the content div with JS when the link is clicked? Thanks for your time.

Upvotes: 0

Views: 191

Answers (1)

Michael Low
Michael Low

Reputation: 24506

Every modern browser (IE8+) supports the hashchange event, you can have your link change the anchor and the hashchange event handler update the content.

HTML:

<a href="#newcontent">change content</a>

Javascript:

    $(window).bind( 'hashchange', function(e) {
        if (location.hash == "#newcontent")
        {
            ...
        }
    });

If you're looking to make a whole webapp on a single page with content changing based on the hash, there are frameworks to help with this - AngularJS, Backbone.js etc.

Upvotes: 3

Related Questions