toggleclass - jQuery toggle class with multiple options -


how can use togglecalss(), in situation this:

<div id="options">    <span data-color="red">red</span>    <span data-color="green">green</span>    <span data-color="blue">blue</span>    <span data-color="black">black</span>  </div>  <div id="target">    <span class="icon home">target</span>  </div>  $('#options').on('click', 'span', function () {    var $iclass= $(this).data('color');    $('#target').find('span').toggleclass($iclass)  }); 

in example above existing class not being replaced clicked one. keeps appending additional classes. here jsfiddle example well: http://jsfiddle.net/5ubud/3/

$('#options').on('click', 'span', function () {     var r = $(this).siblings().map(function () {         return this.getattribute('data-color');     }).get().join(' ');      var t = this.getattribute('data-color');      $('#target').find('span').removeclass(r).toggleclass(t); }); 

http://jsfiddle.net/z7hum/


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -