Jesse Joseph Jesse Joseph - 5 months ago 18
Javascript Question

How to get Tab1 which is an active tab as string into a variable in javascript

<button class="tab-page tab-active" data-tabid="Tab1">Tab1</button>


How to get Tab1 as variable through class Name?

Answer

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")