Reputation: 269
I like to set the color of a php variable with jQuery depending on the content of the value. So this is the code...
<span style="font-size: revert;" id="anart"><strong><?php echo esc_attr($phpvalue); ?></strong></span>
If the output of the variable is "Jackson" the font-color should be yellow, else if "David" it should be blue. How can I implement this in jQuery?
Thank you :)
Upvotes: 0
Views: 122
Reputation: 2731
You can do that without jQuery, using switch case.
var anart = document.querySelector('#anart');
switch (anart.innerText) {
case "Jackson":
anart.style.color = "yellow";
break;
case "David":
anart.style.color = "blue";
}
Upvotes: 0
Reputation: 622
Try this out. Hope it will work.
$( document ).ready(function() {
let $text = $('#anart strong');
if($text.text() === 'Jackson'){
$text.css('color','yellow');
}
if($text.text() === 'David'){
$text.css('color','blue');
}
});
Upvotes: 1