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>
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>
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>
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")
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>
In pure Javascript
document.getElementsByClassName("tab-page tab-active")[0].getAttribute("data-tabid"));
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>
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>