I have a paragraph or h1 with "the color of this item is Magenta" or "this output is Red". The text with color (red, black, cyan, magenta or yellow) could be somewhere on the paragraph or h1 tag.
My code so far.
HTML
<div id="foo">
red magenta yellow black blue
</div>
<h1>magenta toner cartridge</h1>
CSS
.red{
color: red;
}
.magenta{
color: magenta;
}
JAVASCRIPT
$("div:contains('magenta')").each(function () {
$(this)
.html($(this)
.html()
.replace("magenta", "<span class='magenta'>magenta</span>"));
});
$("div:contains('red')").each(function () {
$(this)
.html($(this)
.html()
.replace("red", "<span class='red'>red</span>"));
});
$("h1:contains('magenta')").each(function () {
$(this)
.html($(this)
.html()
.replace("magenta", "<span class='magenta'>MAGENTA</span>"));
});
Question How can I change the background and text color of the word Magenta and Red dynamically based on the text within the element?