lundi 12 décembre 2022

How to convert input field value into checkbox using javascript

I have been trying to create a To-Do list using HTML, CSS and Javascript. From what I found, I am able to get the value of the input field into a fieldset. However, I aim to create a way to convert the input field value from the textfield into the checkbox field.

Please find the codes below for my test project:

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

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

//// Creating functions:
// Adding task into the list:
function addtask()
{
    // Converting the input to 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>

So, How would I be able to get the input value from the textfield and convert it into checkbox?




Aucun commentaire:

Enregistrer un commentaire