17

When clicking on tab A, show content for tab A. Click on tab B, show content for tab B, and so on.

What's the most simple and compatible way of constructing a HTML snippet?

I don't mean to use any libraries here, so none of jQuery or any other libraries.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omg
  • 136,412
  • 142
  • 288
  • 348

9 Answers9

31

If you want to roll your own tab control, you could do something like this:

<html>
  <head>
    <script type="text/javascript">

      function activateTab(pageId) {
          var tabCtrl = document.getElementById('tabCtrl');
          var pageToActivate = document.getElementById(pageId);
          for (var i = 0; i < tabCtrl.childNodes.length; i++) {
              var node = tabCtrl.childNodes[i];
              if (node.nodeType == 1) { /* Element */
                  node.style.display = (node == pageToActivate) ? 'block' : 'none';
              }
          }
      }

    </script>
  </head>
  <body>
    <ul>
      <li>
        <a href="javascript:activateTab('page1')">Tab 1</a>
      </li>
      <li>
        <a href="javascript:activateTab('page2')">Tab 2</a>
      </li>
      ...
    </ul>
    <div id="tabCtrl">
      <div id="page1" style="display: block;">Page 1</div>
      <div id="page2" style="display: none;">Page 2</div>
      ...
    </div>
  </body>
</html>
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Thank you,seems 9,1,3 are all possible values of nodeType ,right? – omg Jun 22 '09 at 22:19
  • According to http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-core.html#ID-1950641247, there are 12 possibilities. Checking for 1 mainly keeps us from getting the attribute and text nodes that may belong to the tab page divs. – Jacob Jun 23 '09 at 00:12
3

Here is a list of different types of tabs plus tutorials on how to build them

Lark
  • 4,654
  • 7
  • 33
  • 34
3

TabTastic is a good guide — it is accessible, and (when JavaScript is not available) fails very gracefully.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

If you want to implement your own tab view, just do it like this:

<html>
    <head>
        <style>
            .tab {
            display:none;
            }
        </style>

        <script type="text/javascript">
          function initTabView(){
            var x = document.getElementsByClassName('tab-view')
            for(var i=0; i < x.length; i++) {
              x[i].onclick = displayTab;
            }

            var prevViewedTab = null;

            function displayTab(e) {
            var idOfTabToDisplay = this.getAttribute("data-tab")

            if(prevViewedTab) {
              prevViewedTab.style.display = 'none';
            }

            var tabToDisplay = document.getElementById(idOfTabToDisplay);
              tabToDisplay.style.display = 'block';
              prevViewedTab = tabToDisplay;
            }

            var defaultTab = document.getElementsByClassName('default-tab')
              if (defaultTab.length) {
                defaultTab[0].style.display = 'block';
                prevViewedTab = defaultTab[0];
              }
          }
        </script>
    </head>

    <body>
        <ul>
            <li>
                <a data-tab="tab1" class="tab-view">Tab 1</a>
            </li>
            <li>
                <a data-tab="tab2" class="tab-view">Tab 2</a>
            </li>
            <li>
                <a data-tab="tab3" class="tab-view">Tab 3</a>
            </li>
            <li>
                <a data-tab="tab4" class="tab-view">Tab 4</a>
            </li>
        </ul>
        <div id="tabCtrl">
            <div class="tab default-tab" id="tab1">This is Tab 1</div>
            <div class="tab" id="tab2">This is Tab 2</div>
            <div class="tab" id="tab3">This is Tab 3</div>
            <div class="tab" id="tab4">This is Tab 4</div>
        </div>

        <script>
            initTabView();
        </script>
    </body>
</html>

A jsFiddle is available here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
naren
  • 14,611
  • 5
  • 38
  • 45
1

Take a look at an example such as this (courtesy of a Google search for 'tabbed view javascript').

You can obviously use this with a little customisation, but it's instructive to take it apart and determine what it's doing. It's basically enabling or disabling <div> using the display style and setting it to block or none

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

The tabs widget in jQuery UI is easy to use: http://jqueryui.com/demos/tabs/.

The jQuery tabs widget works completely on the browser side - content for all tabs are sent on every request, or you could write JavaScript code that uses Ajax to load the tab contents dynamically.

But it might not be appropriate for your use. Consider if you need to control the tabs server-side (that is, a click on a tab sends a new page request to the server - the server constructs HTML that has the visual appearance of tabs).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codeape
  • 97,830
  • 24
  • 159
  • 188
0

If you're open to using JavaScript, jQuery's tab is pretty nice.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane Fulmer
  • 7,510
  • 6
  • 35
  • 43
0

If "simple and compatible" are your only two criteria, I'd suggest the jQuery UI Tabs plugin. Cross-browser, and a cinch to implement.

Mark Hurd
  • 13,010
  • 2
  • 27
  • 28
0

Depending on your ambitions, it could simply be a matter of an unordered list and a number of <div>s (tab contents). Then a simple JavaScript could - by means of getElementById() - set the display property for all the <div>s: none for all except the current.

Alternatively, you could have a look at this.

Edit: Not the only one linking to the jQuery site, it seems :)

jensgram
  • 31,109
  • 6
  • 81
  • 98