JSFiddle - React, Tailwind, and code Playground

by shabuddin

HTML

<div style="background: #00FF00; width: 20px; height: 20px" id="d1"></div>

JavaScript

$(document).ready(function(){
	$('#d1').click(function(){
		alert(getBgColorHex($(this)));
	});
});
function getBgColorHex(elem){
	var color = elem.css('background-color')
	var hex;
	if(color.indexOf('#')>-1){
		//for IE
		hex = color;
	} else {
		var rgb = color.match(/\d+/g);
		hex = '#'+ ('0' + parseInt(rgb[0], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[2], 10).toString(16)).slice(-2);
	}
	return hex;
}