Update the button appearance with a Strikethrough effect using JavaScript

I'm working on a web application that allows users to create to-do items. My goal is to have the text field be crossed out when the checkbox is checked.

When the "add todo" button is clicked, a new item is generated in JavaScript. Then, if the checkbox (referred to as button) is checked, the text should appear crossed out. How can I make this happen?

Thank you in advance!

This is the section of my HTML code where the to-dos are displayed.

<div class="items">

                <h3>Items</h3>

                <div id="itemList">
                </div>

                <button id="addItem" >Add Item</button>

            </div>

This is the constructor function for a new item:

function item(content, list) {
    /* Global attributes */
    this.content = content;
    this.checked = false;
    this.id = 0;
    this.settings = new Settings();
    this.element = $("<div>");
    /* Local attributes */
    var button = $("<input type='checkbox' Class='inputCheck'>"); 
        button.appendTo(this.element);
    var text = $("<input type='text'>"); 
        text.appendTo(this.element); text.val(content);
    var edit = $('<button>Edit</button>'); 
        edit.appendTo(this.element);
    var deleteButton = $('<input type=\'image\' src=\'Images/trash.png\' height=\'20\' width=\'20\'>'); 
        deleteButton.appendTo(this.element);
    var thisObject = this;


    /* Appending item */
    list.addItem(this);
    text.attr("disabled", true);
}

(edit)

Whenever the "add todo" button is clicked, it triggers this function:

$("#addItem").click(function(event){
    var newItem = new item("Default message", listSelected);
});

And when this method is called, the current object's (referred to as thisObject) checkbox status is toggled. The text should also be crossed out at this point.

button.click(function() {
    thisObject.checked = button.is(":checked");
    //thisObject.text => strike
});

Answer №1

The solution provided lacks completeness, but in order to apply a strike-through effect to text, you can utilize the CSS property text-decoration: line-through;.

If you wish to implement striking through text by using checkboxes within an adjacent input field (based on your HTML structure), you can do so with this approach

input[type="checkbox"]:checked + input[type="text"]
.

Another option is to assign a class to the element you want to strike through and modify the class dynamically through Javascript in your application.

As you are utilizing jQuery, here's a demonstration of how you can achieve striking through text either by checking a checkbox (using pure CSS) or clicking a button (with javascript/jQuery) - http://codepen.io/anon/pen/PWoaYZ

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

If the submission fails, then activate the selection of the ID

My dilemma is with a <select> element that initiates an AJAX call to the backend to display or hide additional <select> options based on user selection: $('#select1').on('change', function() { // Fetch fruit details ...

Adjust the x-axis on the Vue.js bar chart

I'm currently working on a Vue.js Laravel application where I am trying to incorporate a bar chart using the ApexCharts module. <apexchart ref="apexChart" :options="chartOptions" :series="chartData" type="bar" ...

What is V8's approach to managing dictionaries?

After attending V8 presentations, it became clear to me that it optimizes constructions such as the one below by tagging a type object: function Point(x, y) { this.x = x; this.y = y; } I am curious about what happens if I were to return an object (JS ...

Endless loops: How React JS components can render indefinitely

Every time I try to render a screen with the Bar component, it seems to get stuck in an infinite loop without even passing any data. I tested importing react-chartjs-2 and that worked fine, loading just once. However, the other bar chart keeps rendering co ...

Tips for utilizing the npm jQuery module effectively

What is the best way to import jquery into multiple modules in node? Should I make it a global variable, or should I use require('jquery') in each module that needs it? I encountered an error while attempting to utilize the package. TypeError: ...

Processing multiple JSON strings passed as a single string - Utilizing Socket.io, NodeJS, and ExpressJS

When receiving data through a socket connected to a third party using websockets, there may be instances where the data contains multiple sets of arrays within a string. The challenge lies in handling this while parsing. For example, I might receive data ...

The frequency of code execution gradually rises with each occurrence of an event

Struggling to articulate this, so please bear with me. My server code seems fine from what I can tell. const latest_time_ref = ref(config.db,"latest_msg/time") const latest_msg_ref = ref(config.db, "latest_msg") io.on("connect&qu ...

Unable to connect to Google Cloud Compute Engine VM using external IP address with Node.js

Attempting some diagnostics, I created a Google Cloud Compute Engine VM (Debian 9). After installing nodejs and setting up a simple script, trying to access the external IP address through my browser resulted in nothing happening. const express = require ...

Is there an alternative module available for running Express APIs from Azure V4 functions, besides the "azure-aws-serverless-express" module?

Encountering an issue where the request URL is coming up as undefined when calling execute from Azure Function Model V4, auzurev4\src\functions\httpTrigger1.ts import { InvocationContext, HttpRequest } from "@azure/functions"; imp ...

The error message "GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs" is alerting about a custom shader issue in

I am working on building a custom shader that will not be rendered. I specifically want to instruct the fragment shader not to write anything, therefore I am not setting gl_FragColor in the code. The shader program performs well on Firefox and Edge, howev ...

Retrieve information by performing a search query on two tables in Laravel and VueJS, using specific conditions

Is there a way to retrieve data from both the Employee and Company tables based on a search by Company name rather than Company Id? These tables have a one-to-many relationship. The current search functionality is working well, but any assistance on implem ...

What is the best way to display the counter result on the cards at the top of an html webpage?

My latest project involves a comparison script using Groovyscript to analyze two distinct files. Embedded within the Groovyscript is HTML code that utilizes CSS and potentially Bootstrap of any version. I have successfully implemented counters with a titl ...

Encountering issues when making API calls from a .NET program

I'm encountering an error when trying to call an API from a .NET application: "Script7002:XMLhttpREQUEST:Network Error 0x80070005, Access Denied." Interestingly, I am able to get the correct response when testing the API with the "Advanced Rest Cl ...

Avoid refreshing the page and maintaining the most recent data inputted

On my HTML page, I am trying to implement a button that submits a form using a function. I have set up field validations, and when I click the submit button, it shows an error message but clears out the values I entered and refreshes the form. What I want ...

Simple solution for storing key-value pairs temporarily in a form using JQuery

Is there an elegant method to temporarily store an array of string values in a form? In my article editing form, users can add tags as string values. I don't want these tags to be persisted until the user saves the entire article, so I require a way ...

Disable checkboxes upon page initialization

I am working with a form that includes checkboxes. Whenever the page loads, clicking on the checkboxes automatically checks them. However, I am looking for a solution where the checkboxes are disabled or not clickable during the page load process. Once th ...

How can we integrate Cordova plugins into Vue-Cordova framework?

Looking to integrate Vue-Cordova with Cordova-plugin-file-opener2 to open PDF files within iOS/Android applications. In the Vue-Cordova setup, plugins related to the device are defined on the data property of the App vue instance: data: function () { ...

Validation of New Relic License Key

Is there a way to verify the validity of a provided New Relic license key in a JavaScript application? I have searched through the documentation but did not come across any API endpoint for this purpose. UPDATE: Just to clarify, we do not have access to ...

Error: The specified key is already present in the object store

I am currently utilizing React 16.3.2, Redux 4, and Dexie 2.0.3. Every time I attempt to store data for the second time, an error message is thrown. Error: ConstraintError: Key already exists in the object store. return dispatch => { db.ta ...

Guide to acquiring and transmitting variables in Node.js

Can anyone guide me on how to pass variables in Node.js? While attempting to fetch and pass some variables to ejs, I encountered the following error: 27| <div id="content"> >> 29| <%=json%> 30| ...