Press the toggle button to switch the image display

Currently learning HTML and attempting to design a dashboard. I want to display the status (ON, OFF, OPEN, or CLOSED) of a room or door using an icon.

I have images for open/close doors and lightbulbs. Need assistance in adding an element to all the existing "li" elements.

Have tried watching tutorials, but they were too complex for me to understand.

<!DOCTYPE html> 
<html lang="en> 
<head> 
<meta charset="UTF-8> 
<meta http-equiv="X-UA-Compatible" content="IE=edge> 
<meta name="viewport" content="width=device-width, initial-scale=1.0> 
<title>Dashboard</title> 
<!-- Add font from Google fonts --> 
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet> 
<!-- Link CSS style sheet to html document --> 
<link rel="stylesheet" href="style.css> 
<!-- Link JavaScript file to html document --> 
<script src="mqttws31.js> </script> 
<script src="dashboard.js> </script> 
</head> 
<body> 
<div class="header> 
<h1>Home Automation Dashboard</h1> 
</div> 
<hr> 
<div id="messages></div> 
<div id="status></div> 
<hr> 
<ul class="dashboard> 
<li class="dashboard_item kitchen> 
<h4>Kitchen</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item frontdoor> 
<h4>Front Door</h4> 
<p>CLOSED</p> 
</li> 
<li class="dashboard_item balconydoor> 
<h4>Balcony Door</h4> 
<p>CLOSED</p> 
</li> 
<li class="dashboard_item livingroom> 
<h4>Livingroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item bedroom> 
<h4>Bedroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item bathroom> 
<h4>Bathroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item studyroom> 
<h4>Studyroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item hall> 
<h4>Hall</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
</ul> 
</body> 
</html>

Answer №1

One way to address this issue is by utilizing the onclick event to activate a function when the tag is clicked. Alternatively, you can attach an Event listener to the tag for a different approach.

It is advisable to employ variables as a state to effectively manage the current status of each item instead of relying solely on the text.

For more information, consider exploring the following resources: https://www.w3schools.com/jsref/event_onclick.asp https://www.w3schools.com/js/js_htmldom_eventlistener.asp

// Utilize variables as the state of each item
var livingRoomState = true;

function toggleKitchenLights() {
  var status = document.getElementById("kitchen-light").innerHTML;
  if (status === "OFF")
    document.getElementById("kitchen-light").innerHTML = "ON";
  else if (status === "ON")
      document.getElementById("kitchen-light").innerHTML = "OFF";
}

var el = document.getElementById("livingroom-btn");
el.addEventListener('click', function() {
      document.getElementById("livingroom-light").innerHTML = livingRoomState ? "ON" : "OFF";
      livingRoomState = !livingRoomState;
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard</title>
    <!-- Add font from Google fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
    <!-- Link CSS style sheet to html document -->
    <link rel="stylesheet" href="style.css">
    <!-- Link JavaScript file to html document -->
    <script src="mqttws31.js"></script>
    <script src="dashboard.js"></script>
  </head>
  <body>
    <div class="header">
      <h1>Home Automation Dashboard</h1>
    </div>
    <hr>
    <div id="messages"></div>
    <div id="status"></div>
    <hr>
    <ul class="dashboard">
      <li class="dashboard_item kitchen">
        <h4>Kitchen</h4>
        <p id="kitchen-light">OFF</p>
        <button onclick="toggleKitchenLights()">Toggle</button>
      </li>
      <li class="dashboard_item frontdoor">
        <h4>Front Door</h4>
        <p>CLOSED</p>
      </li>
      <li class="dashboard_item balconydoor">
        <h4>Balcony Door</h4>
        <p>CLOSED</p>
      </li>
      <li class="dashboard_item livingroom">
        <h4>Livingroom</h4>
        <p id="livingroom-light">OFF</p>
        <button id="livingroom-btn">Toggle</button>
      </li>
      <li class="dashboard_item bedroom">
        <h4>Bedroom</h4>
        <p>OFF</p>
        <button>Toggle</button>
      </li>
      <li class="dashboard_item bathroom">
        <h4>Bathroom</h4>
        <p>OFF</p>
        <button>Toggle</button>
      </li>
      <li class="dashboard_item studyroom">
        <h4>Studyroom</h4>
        <p>OFF</p>
        <button>Toggle</button>
      </li>
      <li class="dashboard_item hall">
        <h4>Hall</h4>
        <p>OFF</p>
        <button>Toggle</button>
      </li>
    </ul>
  </body>
</html>

Answer №2

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Home Control Panel</title> 
<!-- Add font from Google fonts --> 
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet"> 
<!-- Link CSS style sheet to html document --> 
<link rel="stylesheet" href="style.css"> 
<!-- Link JavaScript file to html document --> 
<script src="mqttws31.js"> </script> 
<script src="dashboard.js"> </script> 
</head> 
<body> 
<div class="header"> 
<h1>Smart Home Control Panel</h1> 
</div> 
<hr> 
<div id="messages"></div> 
<div id="status"></div> 
<hr> 
<ul class="dashboard"> 
<li class="dashboard_item kitchen"> 
<h4>Kitchen</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 

<ol class="b"> <!-- in style.css add .b(list-style=none) do same for all lists add images as SVG add width and height as you need -->
   
    <li class="dashboard_item frontdoor" > 
        <img src="./frontdoor.svg" width="50px" height="50px"   alt="">
<h4>Front Door</h4> 
<p>CLOSED</p> 
</li> 
</ol>
<li class="dashboard_item balconydoor"> 
<h4>Balcony Door</h4> 
<p>CLOSED</p> 
</li> 
<li class="dashboard_item livingroom"> 
<h4>Livingroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item bedroom"> 
<h4>Bedroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item bathroom"> 
<h4>Bathroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item studyroom"> 
<h4>Studyroom</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
<li class="dashboard_item hall"> 
<h4>Hall</h4> 
<p>OFF</p> 
<button>Toggle</button> 
</li> 
</ul> 
</body> 
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javax.el.MethodNotFoundException: JSP method cannot be located

While compiling the JSP template, I encountered the following exception: The method getFriendImage() is not recognized I attempted to rebuild the class without success. The src attribute contains a valid link to an image that is functioning properly. ...

Passing a Ruby session variable to a JavaScript tag: a step-by-step guide

I'm currently collaborating with a vendor who utilizes a JavaScript tag for sale attribution, and I need to pass session variables to the tag. The tag appears to be firing properly, as I can see the variables in the logs, but they aren't reflecte ...

Add unique styles to a jQuery-included HTML document

I'm attempting to use jQuery to load an HTML page into the main body of another page. Specifically, I have a div called sidebar_menu positioned in the middle of the page, and I am loading content at the bottom using jQuery. $("#sidebar_menu").load(" ...

Identifying a specific field in a dynamically generated React.js component: Best practices

Currently, I am in the process of developing a form with an undetermined number of sensor fields. The front end has been successfully implemented and now my focus is on extracting user information from these dynamically generated component fields. Here is ...

Addon for Firefox: Image Upload

I am looking for a way to streamline the process of uploading an image to a website through a Firefox Addon. While I know it is possible to use createElement('canvas'), convert Image data to base64, and XHR POST the data, I would prefer to lever ...

Searching for related values in a nested array of objects using JavaScript: A guide

My goal for this project is to thoroughly examine the path along with all nested elements within the Items and assign them to the details variable. However, due to the limitations of the function inside the useEffect, I am unable to check nested items eff ...

Error message: Vue warning - The prop "disabled" must be a boolean type, but a function was provided instead

I have this code snippet that I am currently using <v-list-item> <v-btn @click="onDownloadFile(document)" :disabled=getFileExtention >Download as pdf</v-btn > < ...

Ways to address the issue of "$ is not a function"

Whenever I attempt to upload an image, this error message pops up: $ is not a function The source of the error can be found here: $(document).height(); ...

Using jQuery to compare input with the current select optgroup choices

I'm struggling with looping through options within a form select. The options are grouped into optgroups, and I believe this is where my issue lies as I can't seem to find the correct syntax. My objective is to compare the value entered using .v ...

Issue with Bootstrap navigation bar not displaying on iPad devices

Currently, I am working on creating a custom bootstrap menu that displays properly on an iPad screen size. I have implemented the standard navbar code from Bootstrap with a few tweaks. While it works well on smartphones and laptops, the issue arises when ...

Design a CSS floating div with a captivating bubble effect

My current setup includes a DIV element styled as follows: <div class="modal"></div> .modal { position: fixed; z-index: 999999; right: 310px; top: 67px; width: 431.6px; height: 550px; border-radius: 25px; box-s ...

How do I find out the properties of individual components in Material UI?

Learning material ui has been a challenge for me as I struggle to figure out the properties available for each component. For instance, in a tutorial on YouTube, they used the AppBar component like this: <AppBar title="Enter user details" /> But ho ...

Making modifications to the database will trigger real-time updates in the web interface. How is it possible to achieve this seamlessly?

Can someone please help me understand how to automatically insert news or new events from a Facebook page as an event in a webpage? I am a programmer and currently, the data is retrieved from the database using AJAX without reloading the page. However, I ...

NodeJS: Retrieve Over 10,000 Images From the Server

Attempting to retrieve over 10,000 images from a server led me to create this script: const http = require('http') const fs = require('fs') const opt = { agent: new http.Agent({ keepAlive: true, maxSockets: 5 }), headers ...

Having trouble implementing an onClick event to change a button's CSS to href in Vue.js

When working with a SinglePageApp and using UIKit (UKit), I have found that I need to use a <button> tag instead of an <a> tag. Previously, the <a> tag was written like this: <a type="button" class="uk-button uk-button-link" href="#e ...

Strategies for concealing and revealing content within dynamically loaded AJAX data

I am attempting to show and hide data that is loaded using Ajax. $.ajax({ type: "POST", url: "/swip.php", data: {pid:sldnxtpst,sldnu:sldnu}, success: function(result) { $('.swip').prepend(result); } }); This data gets ...

Bug in canvas rendering for Chrome versions 94 and 95

The Canvas drawing functionality in the Chrome browser seems to have some visible bugs. Here's an example of code that demonstrates this issue: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d&apo ...

Code for retrieving information; some data may not be retrieved

I created a Google Sheets script that iterates through all rows and retrieves API data, then pastes the results in a column all at once. Everything was working fine until I started using the following code snippet instead of referencing a sheet name. var s ...

How can you use Knex to order the results for each WHERE clause in a SELECT query?

When querying a database using knex, the desired results should be ordered in a specific manner. The current code provides the required results but lacks the expected order. knex("FRUITTBL") .select("FruitTag", "FruitName", ...

How to trigger a jQuery function once a v-for loop has completed in VueJS?

Utilizing vue-resource, I successfully retrieve data from my API and incorporate it into my HTML. However, I am encountering an issue where I want to execute a jQuery function after the v-for loop has completed in order for jQuery to recognize elements in ...