Jquery if has class problem
I am using bootstrap tabs and I would like to execute some functionality with javascript if the button is clicked LI
but conditional if it is active does nothing, otherwise do this ...
I came up with this code to check if it isn't has a class="active" since bootstrap tabs adds class="active" to LI for the active tab
, but it doesn't work, it always returns true, what am I doing wrong here?
code
var i = $( "li" ).hasClass( "active" );
$( "li" ).click(function() {
if (i == true ) {
console.log("the tab is already active");
}
else {
console.log("selected");
}
});
here's a jsfiddle demo
+3
source to share
5 answers