Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Custom made Javascript google, change word Open to Close or Open after click

I am creating a small JavaScript function to open or close a div-element, without jQuery but just a small JavaScript and CSS and it is working but I want to achieve the following:

The div starts always hidden when loading the page (done)

If the visitor clicks on "OPEN"-button, the div is displayed and the text "OPEN" must be changed in this save button to "CLOSE" and so on, depending if the div is open or closed we display the text: OPEN or CLOSE (working on it and need help with it)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is an updated Codepen with it:
https://codepen.io/familias/pen/WNYQeqQ

var LetsDoThis = document.querySelector("#toggle");
var LetsDoThat = document.querySelector("#content");

LetsDoThis.addEventListener("click", function() {
  LetsDoThat.classList.toggle("hidden");
});
.hidden {
  display: none;
}
<button id="toggle">OPEN</button>
<div id="content" class="hidden">
  When the "OPEN" button is clicked, the text must change from OPEN to CLOSE, all depending if it is open or closed
</div>

How can I accomplish that ?

>Solution :

See if that fixes it:

var LetsDoThis = document.querySelector("#toggle");
var LetsDoThat = document.querySelector("#content");

LetsDoThis.addEventListener("click", function({ currentTarget }) {
  // true if the class is added or false if it is removed
  let isOpen = LetsDoThat.classList.toggle("hidden");
  
  currentTarget.textContent = isOpen ? 'OPEN' : 'CLOSE'
});
.hidden {
  display: none;
}
<button id="toggle">OPEN</button>
<div id="content" class="hidden">
  When the "OPEN" button is clicked, the text must change from OPEN to CLOSE, all depending if it is open or closed
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading