JSFiddle - React, Tailwind, and code Playground
HTML
<h1 id="myText">This is my text</h1>
<h6 id="itsColor">Its color is: </h6>
<button id="hiThere">Click me</button>
JavaScript
var hexDigits = new Array
("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
document.getElementById('hiThere').addEventListener('click',function () {
document.getElementById('myText').style.color = '#ff0000';
var markup = document.body.innerHTML.trim();
markup = markup.replace(/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/, rgb2hex);
console.log(markup);
});