2

I'm using YUI3 TabView component, and I'd like to be able to get the index of the currently selected tab. I've been looking through the api docs, but can't seem to find the relevant way to do this.

http://developer.yahoo.com/yui/3/api/module_tabview.html

Thanks!

Evert
  • 93,428
  • 18
  • 118
  • 189

1 Answers1

2

"indexOf" actually works if you use the "tabview.get('selection')" as the argument.

Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
    <script type="text/javascript" charset="utf-8"
        src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js">
    </script>
    </head>
    <body>
      <body class="yui3-skin-sam">
      <p id="msg"></p>
    <input type='button' value='Button' id='button'/>
    <div id="demo">
        <ul>
            <li><a href="#foo">foo</a></li>
            <li><a href="#bar">bar</a></li>
            <li><a href="#baz">baz</a></li>
        </ul>
        <div>
            <div id="foo">foo content</div>
            <div id="bar">bar content</div>
            <div id="baz">baz content</div>
        </div>
    </div>
    <script>
var YUI;
YUI().use('event', 'node', 'tabview', function (Y) {
  Y.one('#msg').set('innerHTML', 'message area');

  var tabview = new Y.TabView({srcNode: '#demo'});
  tabview.render();

  var displayIndex = function (tabview) {
    var sel = tabview.get('selection');
    var idx = tabview.indexOf(sel);
    Y.one('#msg').set('innerHTML', 'Selected Tab Index = ' + idx);
  }
  displayIndex(tabview);

  Y.after('click', function(e) {
    displayIndex(this);
  },'body',tabview);

});
    </script>
    </body>
</html>
mjhm
  • 16,497
  • 10
  • 44
  • 55
  • careful with using selection. selection can return a widget instance or an ArrayList if you have multiple selections enabled. A better choice might be tabview.get('activeDescendant'), which will only return a widget instance. tabview/tab use the widget-parent / widget-child extensions. check the docs here: http://developer.yahoo.com/yui/3/api/WidgetParent.html and http://developer.yahoo.com/yui/3/api/WidgetChild.html – bigwebguy Dec 17 '10 at 15:37