"Discover the step-by-step process of transforming an input field value into a checkbox with

I've been experimenting with creating a To-Do list using HTML, CSS, and Javascript. I've managed to capture the input field value in a fieldset so far. However, my goal is to find a way to transform the input field value from the textfield into a checkbox field.

Below are the codes for my trial project:

// Accessing the Add Task button:
var add_task = document.getElementById('TaskAdd');

// Listen for the click event on the add task button:
add_task.addEventListener('click', addtask);

//// Functions creation:
// Adding a task to the list:
function addtask()
{
    // Converting the input into a task list:
    var task = document.getElementById("Taskname").value;
//    document.getElementById("Do1").innerHTML = task;
    
    var taskcheck = document.createElement("INPUT");
    taskcheck.setAttribute("TaskCheck", "checkbox");
//    document.getElementById("Do1").innerHTML = taskcheck;
    document.getElementById("TaskCheck").innerHTML = task;
    document.getElementById("Do1").innerHTML = taskcheck;

    
};
body
{
    /* The image used */
     background-image: linear-gradient(blue, red);
     object-fit: cover;

    /* Full height */
    height: auto;
    width: 100%;

    /* Center and scale the image nicely */
    background-position: center;
    background-size: cover;
    /*background-repeat: no-repeat;*/
}

.center
{
    text-align: center;
    background-color: aquamarine;
}

.background-cover
{
    background-color: aquamarine;
    background-image: url("images/Custom Background(7).jpg");
    background-size: cover;
    background-repeat: no-repeat;
    height: 400px;
    width: 1520px;
}

.background-cover-2
{
    background-color: aquamarine;
    background-size: cover;
    background-repeat: no-repeat;
    font-weight: bold;
}

.background-cover-3
{
    background-color: aquamarine;
    background-size: cover;
    background-repeat: no-repeat;
}

.container
{
    text-align: center;
}

.fieldset2 {
   margin:2px;
   padding:0px;
}
<html>
    <head>
        <title>To-Do List Project</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <!-- Title of the project -->
        <h1 class="center"> To-Do List Program </h1>
        <!-- Creating the background of the cover -->
        <div class="background-cover"></div>
            <!-- Creating container to present the Inputfield to add task for the To-Do list -->
            <div class="container">
                <h1 class="center"> Enter text into the input field to add items into your list </h1>
                <h1 class="center"> Click on the item to mark that the task are complete </h1>
                <h1 class="center"> Click on X to remove the task from To-Do List </h1>
                <input type="text" id="Taskname" name="Tname" placeholder="Input task" size="14">
                <button type="button" id="TaskAdd" onclick="addtask();"> Add </button>
                
                <!-- Creating fieldset to locate the tasks into two sections, first section is the To-Do list -->
                <fieldset class="background-cover-3">
                    <legend class="background-cover-2"> To-Do: </legend>
                    <ul id="Do1" style="list-style: none;">
                        <li>
                        </li>
                    </ul>
                </fieldset>

                <!-- Creating fieldset to locate the tasks into two sections, Second section is the Completed list -->
                <fieldset class="background-cover-3 fieldset2">
                    <legend class="background-cover-2 fieldset2"> Completed: </legend>
                    <ul id="Com1" style="list-style: none;">
                        <li>
                        </li>
                    </ul>
                </fieldset>
                
            </div>
    <script src="main.js"></script>
    </body>
</html>

Could you please advise me on how to extract the input value from the textfield and change it into a checkbox?

Answer №1

If you need to set the value of a checkbox based on a text field, you can use the following approach.

HTML:

<input type="checkbox" id="myCheckbox" value="Hello" />
<input type="text" id="textField" />

Using jQuery:

$("#textField").change(function(){
    $("#myCheckbox").val($(this).val());
    alert("Value of checkbox: " + $("#myCheckbox").val());
});

JavaScript:

let textField = document.querySelector("#textField");
textField.addEventListener("change", function() {
  document.querySelector("#myCheckbox").value = this.value;
});

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

Tips for saving/downloading generated QR codes in React Native

Using this code allows me to generate QR Codes, but I am struggling with saving the generated QR Code in PNG or JPEG format. I have tried a few examples without success and I am continuing to try different methods. import React, { Component } from 'r ...

Flashing text compatibility across all browsers

I'm looking for a way to create text that blinks on all major web browsers. Initially, I attempted using the <blink> HTML tag, but unfortunately, it only works in Mozilla Firefox. Next, I experimented with CSS: <style> .blink {text-deco ...

I'm having trouble getting my JavaScript code to run, can anyone offer any advice on what

Can you provide assistance with this code? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=" ...

Tips for ensuring the border matches the size of the image

I am in the process of creating a website that includes a filter option. My plan is to have the filters displayed on the left side and the items on the right side. To achieve this layout, I have implemented a scrollable div for the items. However, I notic ...

I'm encountering difficulty accessing the Index value within the template's Ref

I'm having trouble accessing the index value inside the templateRef. It shows as undefined in the console. <ng-container *ngFor="let notification of notifications; let i = index"> <ng-template *ngTemplateOutlet="notificationPage ...

What is the best way to shift an image within a div using CSS?

I am currently facing a challenge with moving an image inside a div element. I need to adjust the image by 50 pixels down and 50 pixels left, but I'm having difficulty figuring out how to do this in CSS. I am struggling to find the correct code to con ...

React - the method runs correctly when triggered by state changes, but runs twice when the parent component's state changes

As I work on constructing a page that requires data to be initialized upon mounting, updated based on responses from a websocket server triggered by a button click event, and the ability to disable and re-enable the button with a countdown for the user. M ...

if the current value in the field surpasses the specified value in JavaScript

Looking to incorporate an additional condition into a JavaScript function initial condition if (content.length==elmnt.maxLength) new condition if (content.length==elmnt.maxLength && current form field value > 1400) How can I properly implement the ...

Utilize range slider to refine dataset

I have implemented a jquery datatable along with the range.slider plugin. My goal is to apply the range slider to filter out data in the last column. In my attempt, I am using search.push to accomplish this filtering process. Below is an example of my i ...

Manipulate DIVs by sliding them and resizing their contents with JQuery

Can someone help me with sliding the top div up and down, and the left div left and right? I wrote some code for this but seem to be facing some issues. When I slide the left div, the center content falls down momentarily. Additionally, the code doesn&apos ...

Is it acceptable for Single Page Web Apps to have multiple requests at startup?

I've been dedicated to developing a Single Page Web App (SPA) recently. The frontend is built with BackboneJS/Marionette, while the backend is powered by Java Spring :(. However, I've noticed that the application's start time could be sluggi ...

What potential issues can arise when importing a default export alongside a named export and using webpack in conjunction with babel?

I have two distinct constructors in my codebase: SignUp and GoogleSignIn. They are structured as follows: import SignUp, {gapi_promise} from "./SignUp"; /** * * @param element * @extends SignUp * @constructor */ function GoogleSignIn(element){ SignUp ...

Displaying the outcome of an HTML form submission on the current page

Within the navigation bar, I have included the following form code: <form id="form"> <p> <label for="textarea"></label> <textarea name="textarea" id="textarea" cols="100" rows="5"> ...

A guide on importing gLTF files into three.js

I've been having some trouble uploading a gLTF file using three.js. Despite fixing some errors, I'm still greeted with a black screen. One solution was adding this to my chrome target directory: ‘path to your chrome installation\chrome.exe ...

Presentation Slider (HTML, CSS, JavaScript)

Embarking on my journey of creating webpages, I am eager to replicate the Windows 10 start UI and its browser animations. However, my lack of JavaScript knowledge presents a challenge. Any help in reviewing my code for potential issues would be greatly app ...

JavaScript: Retrieve the Number of Subscribers on the BroadcastChannel

Is there a way to check if a Broadcast channel exists and see how many subscribers it has? We have a product link, and some Chrome tabs are subscribed to a Broadcast channel. We want to determine the number of listeners. const bc = new window.BroadcastCha ...

When I use the AngularJS otherwise function, it sends me to the incorrect route

My routes are defined in the following way: $routeProvider .when('/settings', { templateUrl: 'settingsTemplateURL', controller: settingsCtrl }) .when('/profile', { templateUrl: 'profileTemplateURL', controll ...

The Laravel error message "Attempting to access 'index()' on a null object"

After generating a model in Laravel, I attempted to migrate the table using 'php artisan migrate', but encountered an error message on the terminal: "Call to a member function index() on null" ...

Place the item at the top of the list before scrolling downwards

I'm struggling with what seems like it should be a simple task. My goal is to add new items to the top of a list and have the list scroll down to show the new item. So far, I've managed to create and add a new list item using this code snippet: ...

The React JSX error you encountered is due to the missing return value at the end of the arrow function

After implementing my code, I noticed the following: books.map(({ subjects, formats, title, authors, bookshelves }, index) => { subjects = subjects.join().toLowerCase(); author = authors.map(({ name }) => name).join() ...