I'm quite new to web development and programming . I have two buttons in my webpage. I was trying to change the colour of the buttons based on the text value in the "something" variable as you can see in the code. But when i run this code, both the if statements are executed and both the buttons ( Button 1 and Button 2) turns green instead of one. Any help is appreciated.
(NOTE: even " === " and "==" doesn't work for me ) and I have a script which changes the value for something. So when button 1 or button 2 is clicked , the variable "something "automatically updates with Lights turned On or Lights turned off in text format.
EDIT: Please check the full updated code
<script type="text/javascript">
var something="Lights Turned Off";
window.onload=ChangeColor();
function Lighton(){
document.getElementById('button1').style.backgroundColor = 'green';
}
function Lightoff(){
document.getElementById('button2').style.backgroundColor = 'green';
}
function ChangeColor() {
var something="Lights Turned Off";
if (something= "Lights Turned Off"){
console.log("inside Off function");
Lightoff();
}
if (something= "Lights Turned On"){
console.log("inside On function");
Lighton();
}
}
</script>
FULL CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<?php
$myfile = "./newfile.txt";
$doc1 =file_get_contents($myfile);
echo $doc1; // doc1 has the value inside newfile.txt
// ( ie Lights Turned On OR Lights Turned off)
?>
<style>
.button {
box-shadow: -2px 2px blanchedalmond, -1px 1px orange, -1px 1px orange;
margin-top: 280px;
margin-left: 420px;
background-color:rgb(128, 128, 128);
border: none;
color: white;
padding: 30px 35px;
text-decoration: none;
display: inline-block;
font-size: 16px;
transition-duration: 0.4s;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="x-UA-Compatible" content="ie=edge">
<title>Document ltd</title>
</head>
<body>
<div style = "position:fixed; left:-300px; top:-100px;">
<form method="get" action="http://xxxxxx/ab/my.php" >
<button class="button" id="button1">Lights On</button>
</form>
<form method="get" action="http://xxxxxx/ab/my1.php" >
<button class="button" id="button2">Lights Off</button>
</form>
</div>
<script type="text/javascript">
var something=<?php echo json_encode($doc1); ?>; //takes what's inside doc1.
function Lighton(){
document.getElementById('button1').style.backgroundColor = 'green';
}
function Lightoff(){
document.getElementById('button2').style.backgroundColor = 'green';
}
function ChangeColor() {
if (something=="Lights Turned Off"){
Lightoff();
console.log("inside Off function");
}
if (something=="Lights Turned off"){
Lighton();
console.log("inside On function");
}
}
window.onload=ChangeColor();
</script>
</body>
</html>