I am new to dev and am working on a little project of creating a worshipper counter app for my local mosque.
When the page loads, I want to show only the h1 and the drop down menu. Once an option selected, I want the counter app to display. I have written the function that will toggle the display using an onchange event. However, this is toggling between displaying and hiding no matter the option selected. I can’t seem to find how to display the counter when any value is selected from the dropdown. Please can someone point me in the right direction?
let count = 0 let saveEl = document.getElementById("save-el") let countEl = document.getElementById("count-el") let maxCount = document.getElementById("max") function display() { let app = document.getElementById("counter") if (app.style.display === "none") { app.style.display = "block"; } else { app.style.display = "none"; } let value = document.getElementById("prayers").value; document.getElementById("prayerName").textContent = value } function increment() { count++ countEl.textContent = count if (count >= 50) { console.log(count) maxCount.textContent = "MAX CAPACITY REACHED!" } } function subtract() { count-- countEl.textContent = count if (count < 50) { maxCount.textContent = "" } } function save() { let countStr = count + " - " saveEl.textContent += countStr countEl.textContent = 0 count = 0 maxCount.textContent = "" }
body { background-image: url("zakariyya mosque.png"); background-size: cover; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-weight: bold; text-align: center; } h1 { margin-top: 10px; margin-bottom: 10px; } h2 { font-size: 50px; margin-top: 0; margin-bottom: 20px; } button { border: none; padding-top: 10px; padding-bottom: 10px; color: white; font-weight: bold; width: 200px; margin-bottom: 5px; border-radius: 5px; } #increment-btn { background: blue; } #subtract-btn { background: darkred; } #save-btn { background: darkgreen; } #max { color: crimson; animation: animate 1.5s linear infinite; } @keyframes animate { 0% { opacity: 0; } /* 50% { opacity: 1; } */ 100% { opacity: 1; } }
<h1>COVID19 MUSALLEE COUNTER:</h1> <label for="prayers">Please select prayer:</label> <select name="prayers" id="prayers" onchange="display()"> <option></option> <option value="Fajr">Fajr</option> <option value="Zohar">Zohar</option> <option value="Asar">Asar</option> <option value="Magrib">Magrib</option> <option value="Isha">Isha</option> </select> <h3 id="prayerName"></h3> <br><br> <!-- COUNTER APP HTML --> <div id="counter"> <h2 id="count-el">0</h2> <h2 id="max"></h2> <button id="increment-btn" onclick="increment()">ADD</button> <button id="subtract-btn" onclick="subtract()">SUBTRACT</button> <button id="save-btn" onclick="save()">SAVE</button> <p id="save-el">Previous entries: </p> <p id="fajr">Fajr:</p> <p id="zohar">Zohar:</p> <p id="asar">Asar:</p> <p id="magrib">Magrib:</p> <p id="isha">Isha:</p> </div>
Many thanks in advance! 🙂
Advertisement
Answer
You can take advantage of your “empty” option in the select so that display() can detect that:
let count = 0 let saveEl = document.getElementById("save-el") let countEl = document.getElementById("count-el") let maxCount = document.getElementById("max") function display() { let app = document.getElementById("counter") if (document.getElementById("prayers").value){ app.style.display = "block"; } else { app.style.display = "none"; } let value = document.getElementById("prayers").value; document.getElementById("prayerName").textContent = value } function increment() { count++ countEl.textContent = count if (count >= 50) { console.log(count) maxCount.textContent = "MAX CAPACITY REACHED!" } } function subtract() { count-- countEl.textContent = count if (count < 50) { maxCount.textContent = "" } } function save() { let countStr = count + " - " saveEl.textContent += countStr countEl.textContent = 0 count = 0 maxCount.textContent = "" } display()
body { background-image: url("zakariyya mosque.png"); background-size: cover; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-weight: bold; text-align: center; } h1 { margin-top: 10px; margin-bottom: 10px; } h2 { font-size: 50px; margin-top: 0; margin-bottom: 20px; } button { border: none; padding-top: 10px; padding-bottom: 10px; color: white; font-weight: bold; width: 200px; margin-bottom: 5px; border-radius: 5px; } #increment-btn { background: blue; } #subtract-btn { background: darkred; } #save-btn { background: darkgreen; } #max { color: crimson; animation: animate 1.5s linear infinite; } @keyframes animate { 0% { opacity: 0; } /* 50% { opacity: 1; } */ 100% { opacity: 1; } }
<h1>COVID19 MUSALLEE COUNTER:</h1> <label for="prayers">Please select prayer:</label> <select name="prayers" id="prayers" onchange="display()"> <option></option> <option value="Fajr">Fajr</option> <option value="Zohar">Zohar</option> <option value="Asar">Asar</option> <option value="Magrib">Magrib</option> <option value="Isha">Isha</option> </select> <h3 id="prayerName"></h3> <br><br> <!-- COUNTER APP HTML --> <div id="counter"> <h2 id="count-el">0</h2> <h2 id="max"></h2> <button id="increment-btn" onclick="increment()">ADD</button> <button id="subtract-btn" onclick="subtract()">SUBTRACT</button> <button id="save-btn" onclick="save()">SAVE</button> <p id="save-el">Previous entries: </p> <p id="fajr">Fajr:</p> <p id="zohar">Zohar:</p> <p id="asar">Asar:</p> <p id="magrib">Magrib:</p> <p id="isha">Isha:</p> </div>