It works for me in Chrome if I add spaces after the commas: color == 'rgb(255, 0, 0)'
Since different browsers may return different strings for the color, the easiest thing to do in this case might be to save the string returned by .css('color') immediately after it's set, and then compare the color variable to that:
window.onload = function() {
$('p').css('color', 'rgb(255,0,0)');
var match_red = $('p').css('color'); // save for matching
$('#button').click(function() {
color = $('p').css('color');
if (color == match_red) {
$('p').css('color', 'green');
} else {
$('p').css('color', 'red');
}
})
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p>Will try my jquery in this paragraph</p>
<button type="button" id="button">Click!</button>
That said, your code would be cleaner and easier to debug if you toggled CSS classes instead:
window.onload = function() {
$('#p').addClass('red'); // let's use a unique ID, too
$('#button').click(function() {
$('#p').toggleClass('red green');
})
}
.red {
color: red;
}
.green {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<p id="p">Will try my jquery in this paragraph</p>
<button type="button" id="button">Click!</button>
console.log(color)color == 'rgb(255, 0, 0)'