Skip to content Skip to sidebar Skip to footer

How To Reset Background Colour After It Is Changed By Jquery?

First, a jsfiddle... http://jsfiddle.net/therocketforever/jYba3/11/ // Highlight selected linker link & set all others to default. $('a.linker').click(function(){ $(

Solution 1:

This'll meet all your requirements (bonus included).

$(document).ready(function(){

//  Colours for linksvar colors = ["#fff766","#66cef5","#bd7ebc","#66cc66"];

$("a.linker").click(function(){        
    var $this = $(this).addClass("selected");

    $this.parent('li').siblings().find('.selected')
        .removeClass('selected').css("background-color", "");

    var num = $this.data("colorNum"),
        rand = num;

    while (num === rand) {
        rand = Math.floor(Math.random()*colors.length);
    }

    $this.css("background-color", colors[rand])
        .data("colorNum", rand);
  });
});

Solution 2:

just write this:

$(this).css({'background-color':''});

Solution 3:

Just use jQuery's .css() method to remove the background color:

$(this).addClass('selected')
    .parent('li').siblings().find('.selected')
    .removeClass('selected').css('background-color', '');

Post a Comment for "How To Reset Background Colour After It Is Changed By Jquery?"