Friday, 15 June 2012

jquery - Add class to element -



jquery - Add class to element -

like this, have 2 tabs, when click on 1 it's active, logic. im trying create difference between active , inactive tab, not .css property, wanna' add together specific class clicked tab, this:

$(".tab1").addclass('active');

but, no good. have in mind 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 of don't utilize same id on different elements, id's must unique , seek this:

jquery:

$('.menuitem').click(function() { $('.menuitem').removeclass('active'); //removes active class menu items $(this).addclass('active'); //adds active class clicked 1 });

html:

<div id="tab1" class="menuitem"></div> <div id="tab2" class="menuitem"></div>

css: don't need define same properties active class, define difference:

.active { background-color: red; } .menuitem { width: 170px; height: 70px; float: right; background-color: white; }

jquery class add

No comments:

Post a Comment