Like this, I have two tabs, so when i click on one it's active, logic. Now im trying to make difference between active and inactive tab, but not with .css property, but I wanna' add specific class to clicked tab, like this:
$(".tab1").addClass('active');
but, no good. Also have in mind that I'm using external css file.
<div id="menuitem" class="tab1"></div>
<div id="menuitem" class="tab2"></div>
.active {
width: 170px;
height: 70px;
float: right;
background-color: red;
}
#menuitem {
width: 170px;
height: 70px;
float: right;
background-color: white;
}
First off,
id
s should always be unique.The cause of your issue however, is css specificity. An
id
rule is more specific than a class one. Try this instead.Keep in mind though that this will work to overcome the specificity issue. It does not address the fact that you need to get rid of your duplicated
id
s.CSS Specificity
You can not have same id for two divs in HTML. Try changing your HTML like this.
The other issue might be that you're running your jQuery function before the div element exists.
So either, move the code
below the div's you want to change, or wrap it in a document ready function.
which will wait till the dom has been created before running your code.
First of all don't use same
id
on different elements,id
's must be unique and try this:jQuery:
html:
css: you don't need to define same properties to active class, just define the difference: