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
  • 5,800
  • 1
  • 22
  • 34
Jesse Joseph
  • 49
  • 1
  • 9

6 Answers6

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,751
  • 1
  • 7
  • 15
0

In pure Javascript

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

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
  • 110,383
  • 23
  • 155
  • 178
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.
  • 4,769
  • 4
  • 27
  • 44
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,278
  • 8
  • 49
  • 73
0

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
  • 5,800
  • 1
  • 22
  • 34