Robert Hanson Robert Hanson - 2 years ago 92
Javascript Question

jQuery .hasClass() failed for tab changes

I have this HTML structure :

<div class="tab-pane slide-left padding-20" id="tab1">...</div>
<div class="tab-pane slide-left padding-20 active" id="tab2">...</div>
<div class="tab-pane slide-left padding-20" id="tab3">...</div>

current active tab will have
class, that's why, I want to create conditional function to handle some variables when
is active using this code :

if ($("#tab2").hasClass("active")) {
alert ("yes it's active!!");

but it's not working. no alert message shows up when
is active. how to make this works? thank you.

Please note : tab changes doesn't reload all the page.

Answer Source

You can do something like that.

$('.tab-pane').on('click', function () {
    if ($(this).hasClass("active")) {
        alert($(this).attr('id') + " it's active!!");


Or directly:

$('').on('click', function () {
    alert($(this).attr('id') + " it's active!!");


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download