Making text strike-through when checkbox is marked

I am currently working on developing a To-Do list application using express and EJS middleware. The main feature of this app is to display a list of tasks with checkboxes next to each task added. When a task is completed, the user can mark it as done by checking the checkbox, which will then strike through the text to indicate completion. I have attempted to implement this functionality using JavaScript at the end of my .ejs file, but there seems to be an issue preventing it from working properly.

Below is a snippet of the EJS code:

<% for(var i = listItems2.length -1; i >= 0; i--){ %>
                    <div class="flex">
                      <input type="checkbox" name="checker" id="checkbox_id" class="h-7 w-7 m-3 flex-shrink-0">
                      <p name="para" class="text-[32px] font-[200]"><%= listItems2[i] %></p>
                    </div>
<% } %>

And here is the accompanying JavaScript code:

<script>
  var boxes = document.getElementsByName("checker");
  var paras = document.getElementsByName("para");
  function updateStyle() {
  for (var i = 0; i < boxes.length; i++) {
    if (boxes[i].checked) {
      paras[i].style.webkitTextStroke = "1px black"; 
      paras[i].style.textStroke = "1px black";       
    } else {
      paras[i].style.webkitTextStroke = "none";      
      paras[i].style.textStroke = "none";
    }
  }
}


for (var i = 0; i < boxes.length; i++) {
  boxes[i].addEventListener("change", updateStyle);
}
</script>

Answer №1

To create a strike through effect on the label when the checkbox is checked, you can add a class or style to the label.

You can use the ::before and ::after content elements to indicate that the text has been struck through.

label {
  color: black;
}

#todo {
  accent-color: #777
}

#todo:checked+label {
  color: #777;
  text-decoration: line-through;
  &::before,
  &::after {
    -webkit-clip-path: inset(100%);
    clip-path: inset(100%);
    clip: rect 1px, 1px, 1px, 1px;
    height: 1px;
    overflow: hidden;
    position: absolute;
    white-space: nowrap;
    width: 1px;
  }
  &::before {
    content: "[start of stricken text]";
  }
  &::after {
    content: "[end of stricken text]";
  }
}
<input type="checkbox" id="todo">
<label for="todo">Todo Item</label>

Answer №2

Instead of using the "for" attribute and ID in your label, you can simply wrap the label around the checkbox. Then, put the label text inside a span element and utilize the :checked pseudo-selector along with the direct sibling combinator to apply a strike-through effect using CSS. I have demonstrated the effect by setting one item as checked. Additionally, if you are not satisfied with the default styling of the browser, you can customize the appearance by adding another span with the ::before and ::after styling.

label {
  color: black;
  display: block;
  margin-bottom: 8px
}

input:checked+span {
  color: #777;
  text-decoration: line-through;
}
<label>
  <input type="checkbox" checked>
  <span>Todo Item</span>
</label>
<label>
  <input type="checkbox">
  <span>Todo Item 2</span>
</label>
<label>
  <input type="checkbox">
  <span>Todo Item 3</span>
</label>

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

What is the best way to add clickable icons to google maps using api v3?

Having trouble creating a clickable icon that displays a pop up box of information? Looked through the API docs but can't seem to find what you need? function initialize() { var myLatLng = new google.maps.LatLng(52.288, 0.78); var myOptions = { ...

Achieving background stretching across different browsers with background-size functionality

I'm faced with a dilemma regarding a background image that is 40px wide and 1200px high, which I want to use as the background for a website. Since most users' monitors are not larger than 1200px, I can easily fill the background by setting "back ...

Can you explain the function of this.$nextTick()?

I'm a beginner with Vue.js and I'm trying to wrap my head around the logic of using nextTick in a method. I have two components set up like this: IsTuruIslem.vue <template> ... <t-row> <is-turu-islem-goruntule ref="isTuru ...

Effective method for JSON data parsing

Greetings everyone, I have a question regarding performance implications in JavaScript. I have a JSON query result as follows: [{'name': 'a'}, {'name': 'b'}] For another query, I need to manipulate it to the foll ...

Is it possible to retrieve information from the parent in a Cloud Function?

How can I properly assign the name value inside the parent to a const? exports.myFunction = functions.database.ref('/messages/{pushId}/likes') .onWrite(event => { const name = event.parent.data.val().name; // This approach doesn't ...

How to Populate Object Literal with Multiple Objects in AngularJS

When I click on "Evan", I receive the following response: {"id":1,"name":"Evan"} If I click on "Robert", I will get: {"id":2,"name":"Robert"} Is there a way to modify this code so that it follows the aforementioned steps and generates an object similar ...

Prevent the div from extending beyond the grid boundaries during merging operations

After successfully implementing a jquery drag and drop with selection capabilities to create a grid-like interface, I decided to add a merge function for the div elements. However, I encountered an issue when trying to merge divs B & F in my current code. ...

Repeater employs Endless Scrolling feature within DotNetNuke

I've integrated DotNetNuke into my custom module and I'm following the standard ASP.NET programming techniques (dragging/dropping). The project involves a posting service that uses a repeater with infinite scrolling and a masonry effect. I have f ...

Issue with symbol not functioning on different device

There seems to be a display issue with the ...

Modifying a $scope variable beyond the controller in AngularJS

I am trying to update a variable called $scope.outsidescope outside of the controller in my AngularJS application. The routing is done using $routeProvider, and the $scope.outsidescope variable is located outside of the ng-view. My query is how can I modi ...

Error in Leaflet: Uncaught TypeError: layer.addEventParent is not a function in the promise

Having trouble with Leaflet clusterGroup, encountering the following error: Leaflet error Uncaught (in promise) TypeError: layer.addEventParent is not a function const markerClusters = new MarkerClusterGroup(); const clusters = []; const markers = []; co ...

Informing users of the availability of an iOS app on the website

Is there a simple way to notify iOS users about your app when they visit your website? While I know how to detect user agents and write JavaScript, I'm curious if there is an existing library for this purpose. In the absence of such a solution, are t ...

Enhance the slider's worth

Hey there, just wanted to mention that I didn't write this code myself. Another person did, and I'm a bit lost trying to figure out how to assign values to the three sliders. I don't want a default value; I want each slider to take its value ...

A mistake has been identified: The object could potentially be 'null'. TS2531 for window.document

This is my first time integrating TypeScript into my project. When attempting to access something using window.document.getElementById(), I keep encountering the error: Type error: Object is possibly 'null'. TS2531 I've looked online for ...

Creating a unique visual identity for the collapsing navbar with a dark theme

As a novice in the world of web design, I might be asking a simple question or posting in the wrong forum, so bear with me. In my static blog, I'm using Bootstrap 4 to streamline the design process. While I'm generally satisfied with how it looks ...

Unable to display button image when using both id and style class in CSS

During my transition from Java 7 to Java 8, I've encountered a peculiar issue. Here's a brief explanation: In my FXML file, I have a button defined with style class button-red-big and id btnInput: <Button id="btnInput" fx:id="btnInput" align ...

karma - Plugin not located

Attempting to run JS test cases using karma, but consistently receiving a plugin not found error. Oddly enough, the same configuration file works perfectly for one of my co-workers. Below are the logs: $ karma start karma.conf.js 04 10 2016 17:51:24.755 ...

The method of stamping masonry is not encircling

I recently discovered a new method in Masonry 3 called "stamp" that allows you to fix an element in place. However, I am finding that it is not working as expected. Check out this example from David DeSandro: http://codepen.io/desandro/pen/wKpjs Initial ...

When the screen is minimized, my website doesn't align to the center as expected

When the screen is maximized everything looks great, but when I minimize it, the content doesn't center. I tried using margin: auto; in the "main-div" class, but then everything shifted to the top left corner. So, I added a "wrapper-div" to contain th ...

Having trouble with the onClick function in React?

Here is my simple react code: Main.js: var ReactDom = require('react-dom'); var Main = React.createClass({ render: function(){ return( <div> <a onClick={alert("hello world")} >hello</a> </ ...