1

I want to toggle element visibility. Currently, when clicking button1, it's just hiding, and not toggling.

This is my code and I want to know why it doesn't show button2 after hiding it.

function fonk10() {
var x = document.getElementsByClassName("eben")[0];
    if (x.style.cssText === "display:none") {
        x.style.cssText = "display:inline-block";
    } else {
        x.style.cssText = "display:none";
    }
}
.eben{
  background:green;
  padding:5px;
  display:inline-block;
}

#eben{
  background:green;
  padding:5px;
  display:inline-block;
}
<div id="eben" onclick="fonk10()">BUTTON1</div>
<div class="eben">BUTTON2</div>
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
novruzrhmv
  • 639
  • 5
  • 13

3 Answers3

1

The condition you are using is always false. That's why always else is executing. You have to format the string little bit by inserting space after : and ; at last.

Change

if (x.style.cssText === "display:none") {

To

if (x.style.cssText === "display: none;") {

function fonk10() {
  var x = document.getElementsByClassName("eben")[0];
  if (x.style.cssText === "display: none;") {
      x.style.cssText = "display: inline-block";
  } else {
      x.style.cssText = "display: none";
  }
}
.eben{
  background:green;
  padding:5px;
  display:inline-block;
}

#eben{
  background:green;
  padding:5px;
  display:inline-block;
}
<div id="eben" onclick="fonk10()">BUTTON1</div>
<div class="eben">BUTTON2</div>

You can also use display property like the following way:

function fonk10() {
var x = document.getElementsByClassName("eben")[0];
  if (x.style.display === "none") {
      x.style.display = "inline-block";
  } else {
      x.style.display = "none";
  }
}
.eben{
  background:green;
  padding:5px;
  display:inline-block;
}

#eben{
  background:green;
  padding:5px;
  display:inline-block;
}
<div id="eben" onclick="fonk10()">BUTTON1</div>
<div class="eben">BUTTON2</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

Very nice question: you need inline stiles for the second button. You also need "orthographic precision" for your css roules. But I wouldn't recommend doing it in production.

var x = document.getElementsByClassName("eben")[0];
function fonk10() {console.log(x.style.cssText)

    if (x.style.cssText === "display: inline-block;") {
        x.style.cssText = "display: none;"
    } else {
        x.style.cssText = "display: inline-block;";
    }
}
.eben,#eben{
  background:green;
  padding:5px;
  display:inline-block;
}
<div id="eben" onclick="fonk10()">BUTTON1</div>
<div class="eben"  style="display: inline-block;">BUTTON2</div>
enxaneta
  • 31,608
  • 5
  • 29
  • 42
  • 1
    if you keep the initial if else we don't need inline style because we will fall into the else and starting from there we will have inline styles – Temani Afif Sep 16 '18 at 13:34
  • thanks lot! it's working. i try and i found that i don't need inline style and console log, very thanks again. – novruzrhmv Sep 16 '18 at 13:44
0

Create a Boolean variable explain your element status let us call it show , according to the element if it is displayed then show will be true else if hidden and make the validation with this variable

var show=true;
var x = document.getElementsByClassName("eben")[0];
 function fonk10() {
if (show===false) {
    x.style.cssText = "display:inline-block";
show=true;
    } else {
        x.style.cssText = "display:none";
        show=false;
     }
  }
Osama
  • 2,912
  • 1
  • 12
  • 15
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Sep 16 '18 at 15:03