mardi 19 mai 2020

node.js new element add and remove

When I click on the add button, the text written in textbox should be added on the checkbox And when I click on the remove button, the checked item should be removed

When I click on the add button, the text written in textbox should be added on the checkbox And when I click on the remove button, the checked item should be removed

var ul=document.getElementById('list');
var li;

document.getElementById('add').addEventListener('click',add)
document.getElementById('remove').addEventListener('click',remove)

function remove(){
    //console.log('click remove')
    li=ul.children
    for(var i=0;i<li.length;i++){
        if (li[i].children[0].checked==1) 
            ul.removeChild(li[i])  
    }
}

function add(){
    // console.log('click add')
    var inputType=document.getElementById('input').value;
    var textnode=document.createTextNode('inputType')
    if (inputType==='') {
        return false
    } else {
        //create li
        li=document.createElement('li')

        //create checkbox
        var checkebox=document.createElement('input')
        checkebox.type='checkbox'
        checkebox.setAttribute('id','check')

        //create label
        var label=document.createElement('label')
        label.setAttribute('for','item')

        //add to webpages
        ul.appendChild(label)
        li.appendChild(checkbox)
        ul.appendChild(textnode)
        li.appendChild(label)
        ul.insertBefore(li,ul.childNodes[0])
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add_Remove</title>
</head>
<body>
    <div class="container">
        <input type="text" id="input" placeholder="Add Value"/><br>
        <button type="submit" id="add"> Add </button><br>
        <button type="submit" id="remove"> Remove </button>
    </div>
    <ul id="list">
        <li>
            <label><input type="checkbox" id="Music" />Music</label>
        </li>
        <li>
            <label><input type="checkbox" id="Cricket" />Cricket</label>
        </li>
        <li>
            <label><input type="checkbox" id="Mobile" />Mobile</label>
        </li>
    </ul>
    <script src="addRemove.js"></script>
</body>
</html>

help me to solve my mistake what is mistake




Aucun commentaire:

Enregistrer un commentaire