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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -