My goal is to dynamically read the hexadecimal code from the <span>
tag and set it as the background color
of the same tag using jQuery. Currently, I have code that achieves this with specific variables, but I want to make it more dynamic.
$(document).ready(function() {
var primeCol = $('.colorBlocks div:eq(0) span').text();
$(".colorBlocks div:eq(0) span").css("background-color", primeCol);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span>#0099cc</span>
</div>
<div>
<span>Primary Color 2 is #OOffcc</span>
</div>
I'm looking to modify the code so that it utilizes $(this).text()
and can extract the hex code from any <span>
tag, even if it's embedded within text like in the second <span>
tag. If anyone can provide assistance with this, I would greatly appreciate it as I'm currently facing challenges with it.