I am having problems understanding condition in If statement [or For lopp]. I created div that plays like Menu button. When I click that div all menu including this Menu button is shown but also text [paragraph inside div] changes to "Close menu". I tried to make some if condition in function that is fired up when I click Menu button but it looks like if condition doesn't work like I want to. Here is an example in for loop:
function Show_mobile_menu() {
for (; document.getElementById("Menu_button").style.cssText ="align-self:flex-end;" ; ) {
Func1(); }
}
for (; document.getElementById("Menu_button").style.cssText ="align-self: center;"; ) {
Func2(); }
}
function Func1() {
document.getElementById("Menu").style.display = "flex";
document.getElementById("Submenu").style.display = "flex";
document.getElementById("Menu_button").style.cssText ="align-self: center;"
document.getElementById("Menu_button").getElementsByTagName("P")[0].innerHTML ="Close";
}
function Func2() {
document.getElementById("Menu").style.display = "none";
document.getElementById("Submenu").style.display = "none";
document.getElementById("Menu_button").style.cssText ="align-self: flex-end;"
document.getElementById("Menu_button").getElementsByTagName("P")[0].innerHTML ="Menu";
}
Of course same thing is with if condition:
if ( document.getElementById("Menu_button").style.cssText ="align-self:flex-end;" == true) {
Func1();
}
I tried the same with if textContent = "Menu" then do that... But nothing. As you can see I am learning JavaScript and syntax and meaning still isn't quite clear. I hope someone could clear things up for me.
=will assign the value to the variable. You want to use==when you are checking for equality.forloops. You probably just want to useifstatements. Third, thatifstatement you show should beif (document.getElementById("Menu_button").style.cssText == "align-self:flex-end;"). Fourth, you should use the debugger in your browser and set a break point at thatifstatement, and see whatcssTextactually equals.<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <style> h1 { background-color:pink; } </style> <body> <button type="Button" onclick="Test();">Click me!</button> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <script> function Test() { var Element = document.getElementsByTagName("h1")[0]; if (Element.style.backgroundColor == "pink") { Element.style.backgroundColor = "red"; } } </script> </body> </html>