0

how to hide class with javascript . I get a value if status = 1 then the class show, and if status = 2 then the class is hidden.

<div class="entry-img" id="status">
  <img id="txt_img_content_1" alt="" class="img-fluid">
</div>
Andy
  • 61,948
  • 13
  • 68
  • 95

3 Answers3

1

I thing, that toggle might be to use in your case:

function setStatus(status) {
    document.querySelector("#status").classList.toggle("entry-img", status == 1);
}
div {
  width: 30px;
  height: 30px;
  background: #f00;
  border-radius: 100%;
}
.entry-img {
  background: #0f0;
}
<div id="status"></div>
<button onclick="setStatus(1)">1</button>
<button onclick="setStatus(2)">2</button>
Sven
  • 524
  • 4
  • 10
  • we don't use a button click, just want to display a class that use a status with value which is called using ajax – Luna Project Aug 11 '21 at 10:56
  • it was just an example to show, how it works. Just look inside the example-function "setStatus". The code ".classList.toggle(, )" is simple and you controll, if the css-class is added by the flag (true=add, false=remove). – Sven Aug 12 '21 at 08:41
0

You can do it with the code something like this.

var element = document.getElementById("status");
if(status==2){
  element.classList.remove("entry-img");
}
if(status==1){
  element.classList.add("entry-img");
}

For more information you can refer to this links https://www.w3schools.com/howto/howto_js_remove_class.asp https://www.w3schools.com/howto/howto_js_add_class.asp https://www.w3schools.com/js/js_if_else.asp

0
var divElement = document.getElementById("status");
if(status==2){
  divElement.classList.remove("entry-img");
}
if(status==1){
  divElement.classList.add("entry-img");
}