I am beginner in JavaScript and it looks like I don't understand quite good if statements. I want to change background-color of div from red to blue when div becomes 500px. Hi will get 500px when I click anywhere on body tag. Here is the code:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body onclick="Go();" >
<div id="test"></div>
<style>
#test {
width: 300px;
height: 250px;
background-color: red;
}
</style>
<script>
function Go() {
var div_test = document.getElementById('test').style;
div_test.width="500px";
}
if (document.getElementById('test').style.width="500px") {document.getElementById('test').style.cssText="background-color:blue";}
</script>
</body>
</html>
It looks like that browser instead of understanding part in brackets of if statement as conditional "when that happened", he just create that state/style of div. When page loads div is already blue.