0

How is it possible to extract the tabid data-attribute from the current active tab:

HTML SAMPLE:

<button class="tab-page tab-active" data-tabid="Tab1">Tab1</button> 
Iceman
  • 6,035
  • 2
  • 23
  • 34
Jesse Joseph
  • 49
  • 1
  • 9

6 Answers6

1

Use querySelector to get the element and get tabid value from dataset property.

var activeTab=document.querySelector('.tab-page.tab-active').dataset['tabid'];

console.log(activeTab);
<button class="tab-page tab-active" data-tabid="Tab1">Tab1</button> 
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
1

Vanilla Javascript:

Docs: .getElementsByClassName() & .getAttribute()

document.getElementsByClassName("tab-active")[0].getAttribute("data-tabid"));

jQuery:

Docs: .attr()

$(".tab-active").attr("data-tabid")

jQuery >= 1.4.3

Docs: .data()

$(".tab-active").data("tabid")
Iceman
  • 6,035
  • 2
  • 23
  • 34
0

You can use document.getElementsByClassName("tab-active")

function myFunc() {
  alert(document.getElementsByClassName("tab-active")[0].innerHTML);
  
  }
<button class="tab-page tab-active" data-tabid="Tab1" onclick="myFunc()">Tab1</button> 
Rohit
  • 1,794
  • 1
  • 8
  • 16
0

In pure Javascript

document.getElementsByClassName("tab-page tab-active")[0].getAttribute("data-tabid"));
Canu667
  • 175
  • 1
  • 5
0

It's safer to take into consideration both classes (tab-page and tab-active), so you'd better use document.getElementsByClassName('tab-page tab-active');

var x = document.getElementsByClassName('tab-page tab-active');
var val = x[0].getAttribute('data-tabid');
alert(val);//or do whatever you want with val
 <button class="tab-page tab-active" data-tabid="Tab1">Tab1</button> 
Theodore K.
  • 5,058
  • 4
  • 30
  • 46
0

As an alternative to the pure javascript solutions presented, you can use jQuery's class selector as follows:

$('.tab-page.tab-active').click(function(){$(this).text('Hello');});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="tab-page tab-active" data-tabid="Tab1">Tab1</button>
Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75