backgroundColor in if statement in JavaScript Posted March 19, 2020 I cannot get my if statement to work? I am trying to compare the background color of a table cell within an if statement (i.e. I cannot get the false to work here): <!DOCTYPE html> <html> <body> <table id="tbl" border="1"> <tr> <td>ONE</td> <td>TWO</td> <td>THREE</td> </tr> <tr> <td>FOUR</td> <td style="background-color:yellow" onclick="changeColor()">FIVE</td> <td>SIX</td> </tr> <tr> <td>SEVEN</td> <td>EIGHT</td> <td>NINE</td> </tr> </table> <script type="text/javascript"> function changeColor() { if (tbl.rows[1].cells[1].style.background = '#00f') { alert("true") } else { alert("false") } } </script> </body> </html>
backgroundColor in if statement
in JavaScript
Posted
I cannot get my if statement to work? I am trying to compare the background color of a table cell within an if statement (i.e. I cannot get the false to work here):
<!DOCTYPE html>
<html>
<body>
<table id="tbl" border="1">
<tr>
<td>ONE</td>
<td>TWO</td>
<td>THREE</td>
</tr>
<tr>
<td>FOUR</td>
<td style="background-color:yellow" onclick="changeColor()">FIVE</td>
<td>SIX</td>
</tr>
<tr>
<td>SEVEN</td>
<td>EIGHT</td>
<td>NINE</td>
</tr>
</table>
<script type="text/javascript">
function changeColor() {
if (tbl.rows[1].cells[1].style.background = '#00f') {
alert("true")
} else {
alert("false")
}
}
</script>
</body>
</html>