"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

Are you familiar with using double conditional rendering in a React application?

Currently, I am in the process of creating a small clothing store application using React as a way to expand my skills. One feature I have implemented is a filter by price section. However, I am looking for a solution to handle cases where no items fall wi ...

Integrate Ruby parameter into JavaScript in Rails

I have a div that I want to turn into a link to another page. Typically, we achieve this using link_to link. <% for item in @items %> <%= link_to "Item", item %> # -> or <%= link_to(item) %> <% end %> However, I need the ent ...

Complete the form by submitting it along with the other forms once they have been added to the page

I've encountered a situation where I have a page containing a search button. Upon clicking the submit button, an ajax request is sent to fetch JSON data from the controller and populate my page with multiple product boxes. Each box represents a new fo ...

Arranging divs with HTML and CSS

I am currently facing issues with the positioning of 3 scripts in my HTML file. The vertical gauge is overlapping the second circular gauge, despite trying different positioning options to no avail. How can I adjust the layout so that the vertical gauge a ...

Creating a unique function to map an array in VueJS specifically designed for table manipulation

I am currently working on displaying and sorting data in a bootstrap table within VueJS. My goal is to change the date format within an array retrieved from an API endpoint. The original date format is in "January 21, 2010" and I need it to be in "MM/DD/Y ...

The submit button fails to function properly in the presence of multiple fields

Encountering an issue where the Submit button becomes unresponsive when multiple addresses are generated. To resolve this, I attempted to use JavaScript. @foreach ($userDetails as $addr) <div class="order_container"> <input type="hid ...

Efficiently managing modules with requirejs and Backbone.Marionette

After organizing the file structure of my web app, utilizing RequireJs and Backbone.Marionette, it now looks like this: |- main.js |- app.js |- /subapp1 |- subapp1.js |- subapp1.router.js |- /subapp2 |- subapp2.js | ...

Utilizing AJAX to submit an array of form inputs and securely storing the data in MySQL using

I have a set of code below that has been functioning well for a single input field. However, I also have additional code that adds extra fields to the form when a div with id #addForm is clicked, and these inputs are named "itemName[]". <script> ...

PHP Calculator with Dynamic Calculations

I need to create an order form that updates the tax and total automatically once a quantity is entered into the text box. Should I use PHP or JavaScript for this functionality? Let's assume, for instance, that the tax rate is 0.3% My requirements: ...

Is the fixed header see-through?

I have a query regarding my website design. Currently, the fixed header on my site is transparent and when I scroll down, the content overlaps the header. However, I want the header to remain above the content. Can anyone provide assistance with this issue ...

The geolocation navigator is not defined

Currently, I am in the process of developing an AngularJS application that I plan to convert into a mobile app using Cordova. My goal is to incorporate Cordova's geolocation plugin into the app, but I have encountered an issue where it returns undefin ...

Having trouble getting routing to function properly with react-router-dom

I'm currently assisting a friend with completing a React Project. I'm facing an issue while trying to set up routing using react-router-dom. The components inside the <switch> tag are not functioning properly. Below are snippets of my code: ...

Is there a way for me to scroll and bring the block up to the header when the button is clicked?

My goal is to create a functionality where clicking on the button with the class .booking__button will smoothly scroll the block up under the header. The position of the block should remain consistent, only the scrolling effect should be visible to the use ...

Getting a URL to redirect after a successful login via an AJAX request in PHP

I've been trying to figure out how to redirect the URL after a successful login using an Ajax call in PHP. Can someone please review my code and point out any mistakes? Here is the content of the registration.php file located at http://localhost:8080 ...

Looking to validate each input field individually using jQuery?

I am in need of validating all form inputs in the keyup function without having to write validation for each individual input. Is there a way to achieve this using methods like each();? Apologies for what may seem like a silly question. ' ...

Enable row selection in UI-Grid by clicking on a checkbox with AngularJS

I am a newcomer to angular js and I am looking to have the checkbox automatically selected when clicking on a row to edit that specific cell. I have implemented a cell template to display the checkbox in the UI-grid, but now, when I select a row, the row i ...

Empty req.params in nested ExpressJS routers

I have a unique routing system that relies on the directory structure of my 'api' folder to automatically configure routes. However, I encountered an issue where the req.params object is undefined in the controller when using a folder name as a r ...

Leveraging Forms for Entering Google Maps Information

Recently, I've been working on an app that aims to generate a custom map based on user input from a form. If you'd like to test the functionality yourself, head over to this page. After filling out all required fields and hitting "Submit", the g ...

Experiencing an excessive number of re-renders can be a common issue in React as it has limitations set in place to prevent infinite loops. This

I have integrated React context to access the login function and error from the context provider file for logging into the firebase database. I am trying to display any thrown errors in the app during the login process. However, I encountered an issue whe ...

What is the best way to load the route calculation dynamically?

I am struggling with calculating the Google Maps route dynamically. The console shows an error stating that 'calcularRuta' is not defined: Uncaught ReferenceError: calcularRuta is not defined at HTMLInputElement.onclick (index.html:1) Despi ...