Changing contents by clicking on links eg: href="#tab2"

Discussion in 'JavaScript' started by ads2help, Jan 21, 2009.

  1. #1
    I came across a site last week and but I forgot what's the site url.

    It has a few tabs on top, each with links which looks like:

    <a href="#tab1">Tab 1</a>
    <a href="#tab2">Tab 2</a>
    <a href="#tab3">Tab 3</a>

    When I click at Tab 2, it shows tab 2 and hides tab 1 and tab 3.

    So I think it has something related to the # thing. Maybe a function that monitor the changes of #??

    I am 100% sure that the link do not have onclick="function" on them, just a simple <a href="#tab1">Tab 1</a>

    Something like Gmail..
    Gmail inbox is : url#inbox
    Gmail spam is : url#spam

    How can i do that? Thank you.

    BTW, for your information, its not things like this
     
    ads2help, Jan 21, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    well - you can do all sorts... via a framework - say mootools or jquery, you'd do something like:

    <a class="tab" href="#tab1">Tab 1</a>
    <a class="tab" href="#tab2">Tab 2</a>
    <a class="tab" href="#tab3">Tab 3</a>
    ...
    <div class="tabs" id="tab1">content in tab 1</div>
    <div class="tabs" id="tab2" style="display: none">content in tab 2</div>
    <div class="tabs" id="tab3" style="display: none">content in tab 3</div>
    
    <script type="text/javascript">
    // for mootools 1.2.1
    window.addEvent("domready", function() {
    $$("a.tab").addEvents({
        "click": function(e) {
            e.preventDefault(); // stop click from changing url.
            var target = this.get("href").replace("#", ""); // gets the tab
            // close all tabs
            $$("div.tabs").setStyle("display", "none");
            // open the one that's clicked
            $(target).setStyle("display", "block");
        }
    }); // end addEvents
    
    }); // end domready
    </script>
    PHP:
    Admitedly this is a semantic way of doing things but it is now also acceptable to do something like:
    <a class="tab" href="#" data-id="tab1">Tab 1</a>
    ... then later on in the JS
    var target = $(this.get("data-id"));
    PHP:
    the data-nnn is a namespace introduced by HTML 5 for element storage and should validate correctly.

    also, all of the above can be easily done w/o a framework also.
     
    dimitar christoff, Jan 22, 2009 IP
    ads2help likes this.
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Thanks for spending time on my post. Thanks!

    + rep for you
     
    ads2help, Jan 22, 2009 IP