0

I would like to change display property of this input (actually of div which contains it) after onclick ("Click here"). This part goes well ... but after clicking again I want it to be hidden again (has display="none" instead of "block" again and so on) and here I have difficulties. I've tried classList.toggle but ... I' don't want to change classes, I want to change just one property. I know there is also possibility of creating my input field by using Javascript but I presume I'll come to a deadlock again in the same point.

 <form role="form">
      <p id="mag">Click here!</p>

  <div id="switch" class="xxx"style="display:none;" >
  <input id="sr"class="offf" autocomplete="off" placeholder="Search" type="text" >

  </div>
  </form>   



document.getElementById("mag").addEventListener("click", function toffi(){

  document.getElementById("switch").style.display="block";

       console.log(document.getElementById("switch").style.display);


});

http://codepen.io/zeeebra/pen/RgRYxK

1 Answer 1

4

Try this:

document.getElementById("mag").addEventListener("click", function toffi(){
  var sw = document.getElementById("switch");
  if (sw.style.display === "block") {
    sw.style.display = "none";
  } else {
    sw.style.display = "block";
  }
  console.log(sw.style.display);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.