Skip to content

Instantly share code, notes, and snippets.

@ali-master
Created December 29, 2016 01:10
Show Gist options
  • Save ali-master/05dfa76b9905cd5a229fb64f2b68607b to your computer and use it in GitHub Desktop.
Save ali-master/05dfa76b9905cd5a229fb64f2b68607b to your computer and use it in GitHub Desktop.
Get Background-Color of Elements with jQuery

Get Background-Color of Elements with jQuery

(function($){
    $.fn.getBackgroundColor = function() {
        var self = $(this);
        var bgColor = "";
        while(self.prop("tagName").toLowerCase() != "html") {
            bgColor = self.css("background-color");
            if(bgColor != "rgba(0, 0, 0, 0)" && bgColor != "transparent") {
                break;
            }
            self = $(this).parent();
        }
        return bgColor;
    }
})(jQuery);

Usage

var bgColor = $("body").getBackgroundColor();

console.debug(bgColor); // return String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment