benjo benjo - 7 days ago 4
jQuery Question

check if <font> html tag has color and add label | js | jq

I have this HTML:

<font color="#ff0000">מתחנת חוף הכרמל</font>


I want to check if it has the
color
style and if it does I want to add a
label
to it. This is what I tried to do with no success:

if ($("font").css('color') == '#ff0000' ) {
$("font").attr('role':'heading');
} else {
console.log('not working');
}


Any advice? If it's possible also with pure JS it will be great.

Answer

try this:

<font style="color:#ff0000;">מתחנת חוף הכרמל</font>

var col = document.getElementsByTagName('font')[0].style.color;
 if(col=='rgb\(255, 0, 0\)') {
   document.getElementsByTagName('font')[0].setAttribute('role','heading');
 } else {
   console.log('do stuff...');
 }

check the fiddle for it: https://jsfiddle.net/r2sb0hjn/