0

I'm trying to get the value of cookies enabled to turn red if false or green if true but I've only been able to change the color regardless if it's true or false, is there another way around this using JS?

Or if you were to take it one step further, is it possible to put in an image (red X or green Tick) after the value?

HTML

<table class="table"> <tr> <td>Cookies Enabled:</td> <td id="cookie"></td> </tr> </table> 

JS

var Cookies; { if (navigator.cookieEnabled) { Cookies = "True"; } else { Cookies = "False"; } } window.onload = function () { document.getElementById("cookie").innerHTML = Cookies; } 

1 Answer 1

2

This should be simple enough:

window.onload = function() { var cookieElem = document.getElementById('cookie'); if (navigator.cookieEnabled) { cookieElem.classList.add('green'); cookieElem.innerHTML = 'True'; } else { cookieElem.classList.add('red'); cookieElem.innerHTML = 'False'; } }
#cookie.green { color: green; } #cookie.red { color: red; }
<table class="table"> <tr> <td>Cookies Enabled:</td> <td id="cookie"></td> </tr> </table>

Sign up to request clarification or add additional context in comments.

1 Comment

Funny, when I disable cookies (I wanted to test this), Run code snippet is no longer available to me. Why does it need cookies?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.