Tabbed Content / Posts /

Identify which tab is active

I have some common controls in the div class "tabcontents", before the first tab , div "view1", is defined.  One such control is a button, which when clicked needs to know which tab is active.  Can this be done?
Thanks for sharing this awesome code and solution!

Fred  10 years ago   viewed: 11953    

4 Answers

You need programming with JavaScript based on checking the className of the tabs. For example:

var lis = document.getElementById("theULid").getElementsByTagName("li");
for(var i=0; i<lis; i++){
if(lis[i].className == "selected")
return i; //or return lis[i], or return lis[i].getElementsByTagName("a")[0]
Joe   10 years ago
Thanks Joe!
  While I don't know much about JavaScript, you did put me on the right path to discovery!  Sad to say, I'm limited to writing in VB script, and I had to take steps to get your routine into a VB script equivalent!
        Set LI = document.getElementById("Allofthetabs").getElementsByTagName("li") : iCount = 0
For Each LIItem IN LI
iCount = iCount + 1 : if LIItem.ClassName = "selected" then Exit For
When the above loop exists, iCount holds the number of the selected tab..., which is to say, exactly what I needed!
Thanks again for your code and example!

Fred   10 years ago

The advanced version, jQuery Tabs, has an event handler onTabSelected. The first parameter passed to the onTabSelected function is the clicked tab. So you can get the clicked tab there. For details please read the

Milo   8 years ago
We can check the active tab by checking the value of the active tab state variable. For example, if activeTab is 'tab1', then the first tab is active; if activeTab is 'tab2', then the second tab is active, and so on. duck life
Palmer Hughes
  11 months ago


Your name*
(Optional. Used to modify this post afterwords)
+ =  

Ask your Own Question

  • If your question is related to the topic of this post, you can post your question to this page by clicking the "Post a reply" button at left;

  • When you want to start a new page for your question: