Switching text appearance upon checking the checkbox

I am currently working on a Vue.js app for a To Do List. I have a feature where you can add tasks to a list, each with its own checkbox. I want to implement a feature that puts a line-through text style on an item once its checkbox is marked. Can anyone provide a faster and easier way to achieve this on Vue.js? Below is a snippet of my code:

Vue.createApp({
    data(){
        return{
          placeholder: 'Start typing',
          inputvalue: '',
          notes: []
        }
    },
    mounted() {
        this.notes = JSON.parse(localStorage.getItem('note')) || [];
      },
      watch: {
            notes: {
                handler: function() {
                    localStorage.setItem('note', JSON.stringify(this.notes));
                },
                deep: true
            }
        },
    methods: {
        addnewtask(){
            if (this.inputvalue !== ''){
                this.notes.push(this.inputvalue)
                this.inputvalue=''
            }
        },
        removetask(index){
            if (confirm('Do you really want to delete?'))
            this.notes.splice(index, 1)
        }
    }
}).mount(app)
<!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>To Do List</title>
</head>
<link rel="stylesheet" href="style.css">
<style>
    [v-cloak] {
        display:none;
    }
</style>
<body>
    <div class="container" id="app" v-cloak>
      <div class="card">
          <h1>To Do List</h1>
          <div class="form-control">
             <input
                 type="text" 
                 v-bind:placeholder="placeholder" 
                 v-model="inputvalue"
                 v-on:keypress.enter="addnewtask"
              />
              <button class="btn" v-on:click="addnewtask">Add Task</button>
            </div>
            <hr />
            <ul class="list" v-if="notes.length !== 0"...>
                <li class="list-item" v-for="(note, index) in notes" v-bind:key="note">
                    <div>
                        <input type="checkbox"/>
                        {{index+1}}) {{note}}
                    </div>
                    <button class="btn danger" v-on:click="removetask(index)">Delete</button>
                </li>
                <hr />
                <li>
                    <strong>Total: {{notes.length}}</strong>
                </li>
            </ul>
            <div v-else>No task exist, please add first one.</div>
      </div>
    </div>
    <script src="https://unpkg.com/vue@next"></script>
    <script src="Vue3.js"></script>
</body>
</html>

Answer №1

Learn how to implement conditional rendering with Vue.js:

<span :style="todo.completed ? 'text-decoration: line-through' : ''">{{todo.title}}</span>

Check out this basic example on the Vue Playground

<script setup>
import { ref } from 'vue'
const todos = ref([
  {title: "Milk", completed: true},
  {title: "Bread", completed: false},
  {title: "Cheese", completed: true},
  {title: "Chicken", completed: false},
])
</script>
<template>
  <template v-for="todo in todos" :key="todo.title">
    <p>
      <input type="checkbox" v-model="todo.completed" /> <span :style="todo.completed ? 'text-decoration: line-through' : ''">{{todo.title}}</span>
    </p>
  </template>
</template>

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

Eliminate the header top margin in Bootstrap

HTML <div class="container-fluid"> <div class="row"> <div class="page-header header_site"> <h1><font>ABC Company</font></h1> </div> </div> </div> CSS Code: .header_site { b ...

Error: Attempting to display API data in a CardView using NativeScript-Vue results in a TypeError stating that property 'endpoint_link_1' is undefined

Greetings, I am a beginner in NativeScript-Vue and JavaScript. I do not have extensive experience with heavy Javascript coding. I am facing an issue with displaying data fetched from an API in CardViews. Below is the code snippet I attempted: <template ...

Retrieve the data from every dropdown menu

How can I retrieve the selected values from all created selects when a button is clicked? I have attempted using both refs and v-model, but neither of them are functioning as expected. <div v-for="attribute in attributes" class="col"> {{ a ...

Conflicting Transformation Properties Causing CSS Issues Within a Single Element

I'm currently working on a user interface where users can drag and drop a box with a red outline to position it on the screen within a black box. See the UI here Alternatively, users can also move the box by adjusting the inputs on the right side. ...

Paste the URL into your clipboard without relying on JavaScript

Is there a way to implement a copy to clipboard function for email templates without relying on JavaScript? ...

Creating a login screen without using App.vue components requires a different approach and can be achieved through alternative methods

I have been working on a Single Page Application (SPA) that features a login screen and other views. One issue I encountered was that the login screen view was appearing in the Navigation bar when it shouldn't be there. To solve this, I implemented Ro ...

vertically centering a div specifically on laptops

What is the best way to vertically center a div on lap-tops? (...) body {padding: 4% 16%;} body {top:0px; left:0px; bottom:0px; right:0px;} body {border: 12px solid darkred; border-style: double;} body {background-color: #FAFCB4;} p {font-size: 80%; text- ...

Tips for stopping an accordion from toggling in Bootstrap when a user clicks on a specific section of the toggle button

I am currently utilizing bootstrap for my project. The accordion feature I have implemented is standard, but I am looking for a way to customize its behavior. Specifically, when a user clicks on elements with the class 'option', I want it to sele ...

The Azure pipeline configured for a Vue application is experiencing issues with rendering the webpage

I've been working on deploying my Vue app, hosted in an Azure repository, to an Azure app service using the provided pipeline by Azure. The deployment process goes smoothly with no errors, yet when I access the URL of my Azure app service, I encounter ...

Entire body encompassing table

I am trying to create an HTML page with a table that spans the entire body and displays scrollbars when needed for both horizontal and vertical scrolling. Here is my current styling for the table: table { box-shadow: inset 0 0 10px #000; position: ...

Upon logging in, Vuejs does not detect the localStorage item until after a page refresh

I'm currently working on a Vue app that requires authentication. Upon successful login, the backend provides a jwt string. The issue I'm facing is that the projects are not being fetched after logging in. Strangely, if I manually refresh the p ...

What is the best way to blend a portion of an image into a div?

Having a bit of trouble with overlapping my image onto another div in my current project. When you take a look at what I've been working on, you'll see there's an image that says "That's me!" and it needs to overlap the left side div. I ...

Enhance user experience with dynamic color changes in JavaScript

Looking to create a navigation menu with unique colors for each selected state? Check out the code below! After searching extensively, I stumbled upon this snippet. While it only includes one selected state, you can easily customize it for three different ...

Having trouble reaching the instance of createApp that was exported in Vue 3

I am facing an issue with my createApp instance in the vuetify plugin. In my main file, I have imported createApp from vue and used it to create an app with App.vue. I also imported the vuetify plugin as shown below: import { createApp } from 'vue&apo ...

Crafting an interactive saturation effect for mouseover interactions in a circular design

I'm looking to create a unique hover effect for my images. I have both a desaturated version and a full color version of the same image. My idea is to have mousing over the desaturated image reveal a circle spotlighting the color version underneath, a ...

difficulty encountered when passing parameters in functions such as setInterval

Hi everyone, I need some help with my code. Feel free to check it out here Currently, I'm working on implementing multiple circular progress bars that can function dynamically. The goal is to be able to add additional progressCircle_# objects with di ...

Vue 3 - Non-reactive object properties

How do I ensure that userInfo.value.email is reactive in this Vue3 component: <script setup> import { useUserStore } from '../store/user'; import { storeToRefs } from "pinia"; const userStore = useUserStore() const ...

What is the best way to maintain scrollbars on mobile devices?

I'm currently building a cross-platform application using Angular 4 that needs to work seamlessly on both mobile and desktop devices. Is there a special trick to prevent the scrollbars from disappearing when viewing this page on a mobile browser? I&ap ...

Ace code editor: A guide to implementing hover error messages

Using an Ace editor, I want to enhance my website by highlighting specific code ranges and displaying error messages when they are hovered over, just like in the example image. I'm curious if this functionality can be achieved through the Ace editor ...

Random image generator using jQuery on the home page body

Hey guys, I'm working on a cool feature for my Wordpress site where a random background image loads every time the homepage is refreshed. Check out the code snippets below: First, here's what I have in my style sheet (style.css): body.home { ...