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>
3
  • you want to hide the div or just remove the class, you can use toggle or show or hide Commented Aug 11, 2021 at 8:08
  • 1
    Where's the value of status coming from? Commented Aug 11, 2021 at 8:08
  • This may help with finding your answer: stackoverflow.com/a/18414401/13566068 Commented Aug 11, 2021 at 8:10

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

2 Comments

we don't use a button click, just want to display a class that use a status with value which is called using ajax
it was just an example to show, how it works. Just look inside the example-function "setStatus". The code "<dom-element>.classList.toggle(<css-class>, <flag>)" is simple and you controll, if the css-class is added by the flag (true=add, false=remove).
0
var divElement = document.getElementById("status");
if(status==2){
  divElement.classList.remove("entry-img");
}
if(status==1){
  divElement.classList.add("entry-img");
}

Comments

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

1 Comment

also, if you get an element, it won't be a number

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.