css - Jquery: change CLASS between multiple DIVs -
i have such task: create function jquery change css-class selected div. class="xxx" can repeated many times class="yyy" once.
like this:
<div onclick="change_css()"class="xxx">text</div> <div onclick="change_css()"class="xxx">text</div> <div onclick="change_css()"class="xxx">text</div> <div onclick="change_css()"class="yyy">text</div> <div onclick="change_css()"class="xxx">text</div>
so, means function need find first class="yyy" , change class="xxx" , secondly modify class "yyy" activated onclick
...
please, me achieve functions... know need use somehow .addclass
, .removeclass
function change_css() { /// tricks here }
thank every suggestion!
i suggest different, more jquery approach. drop "onclick" attributes <div>
s , use class (mydiv
example) on relevant <div>
s: here's working jsfiddle
<div class="mydiv xxx">text</div> <div class="mydiv xxx">text</div> <div class="mydiv xxx">text</div> <div class="mydiv yyy">text</div> <div class="mydiv xxx">text</div>
then attach click event using jquery:
$(function(){ $('.mydiv').click(function(){ // when .mydiv clicked $('.mydiv.yyy').removeclass('yyy').addclass('xxx'); // replace former yyy xxx $(this).removeclass('xxx').addclass('yyy'); // add class yyy current element instead of class xxx }) })
Comments
Post a Comment