简体   繁体   中英

Toggle Checkbox upon clicking on Span

I'm working on an assignment that needs to, upon the click of a span element's text in an input div, output the same text in an output div. That part I've done successfully, but to the left of each span element within the input div is a checkbox that needs to also be checked upon the click of said span element.

I am not allowed to target each individual checkbox with its own unique ID because I will be adding in newer checkboxes and span elements later with the press of a button and prompt. This is an assignment on event delegation.

I will then need to be able to uncheck the checkbox and remove the appended output, but first things first, I cannot figure out how to target the checkboxes. The only thing I can think of is to somehow say that whatever index number of said span element was clicked would be the index number of said checkbox is clicked, but I'm unsure if that is the best method as well as how to go about doing that.

Also, this assignment should not have any JQuery involved. My next project is actually to redo this assignment in JQuery. Any help would be appreciated!

HTML:

<!DOCTYPE html>
<html>

<head>
    <title>Canvas Document Object Model Exercise</title>
    <link rel="stylesheet" href="canvas_stylesheet.css">
</head>

<body>
    <div id="input">
        <form>
            <input class="checkbox" type="checkbox"><span class="values">Apple</span></br></br>
            <input class="checkbox" type="checkbox"><span class="values">Mango</span></br></br>
            <input class="checkbox" type="checkbox"><span class="values">Grape</span></br></br>
            <input class="checkbox" type="checkbox"><span class="values">Strawberry</span></br></br>
            <input class="checkbox" type="checkbox"><span class="values">Cherry</span>
        </form>
    </div>

    <div id="output"></div>

CSS:

#input {
    width: 250px;
    height: 300px;
    float: left;
    padding: 20px 0 30px 15px;
    border-style: solid;
    border-color: black;
    border-width: 1px;
}

.values {
    display: inline;
}

/*
#input form input {
    padding: 20px 20px 20px 20px;
}
*/

#output {
    width: 225px;
    height: 326px;
    float: left;
    border-top: 1px solid black;
    border-right: 1px solid black;
    border-bottom: 1px solid black;
    padding: 4px 20px 20px;
}

JS:

window.onload = UncheckAll;
function UncheckAll() { 
    var w = document.getElementsByTagName('input'); 
    for (var i = 0; i < w.length; i++) { 
        if (w[i].type == 'checkbox') { 
            w[i].checked = false; 
        }
    }
}

document.getElementById("input").addEventListener("click", function(e){
    if (e.target.nodeName === "SPAN") {
        var node = document.createElement("P");
        var textnode = document.createTextNode(e.target.innerHTML);

        node.appendChild(textnode);
        document.getElementById("output").appendChild(node);
        node.setAttribute("class", "outputItem")
    }
});

Just surround your checkboxes elements with label, like I did here.

Ps: plese never write a br element like this </br> , its <br> with no slash at all

 window.onload = UncheckAll; function UncheckAll() { var w = document.getElementsByTagName('input'); for (var i = 0; i < w.length; i++) { if (w[i].type == 'checkbox') { w[i].checked = false; } } } document.getElementById("input").addEventListener("click", function(e){ if (e.target.nodeName === "SPAN") { var node = document.createElement("P"); var textnode = document.createTextNode(e.target.innerHTML); node.appendChild(textnode); document.getElementById("output").appendChild(node); node.setAttribute("class", "outputItem") } }); 
 #input { width: 250px; height: 300px; float: left; padding: 20px 0 30px 15px; border-style: solid; border-color: black; border-width: 1px; } .values { display: inline; } /* #input form input { padding: 20px 20px 20px 20px; } */ #output { width: 225px; height: 326px; float: left; border-top: 1px solid black; border-right: 1px solid black; border-bottom: 1px solid black; padding: 4px 20px 20px; } 
 <!DOCTYPE html> <html> <head> <title>Canvas Document Object Model Exercise</title> <link rel="stylesheet" href="canvas_stylesheet.css"> </head> <body> <div id="input"> <form> <label> <input class="checkbox" type="checkbox"><span class="values">Apple</span> </label> <br><br> <label> <input class="checkbox" type="checkbox"><span class="values">Mango</span> </label> <br><br> <label> <input class="checkbox" type="checkbox"><span class="values">Grape</span> </label> <br><br> <label> <input class="checkbox" type="checkbox"><span class="values">Strawberry</span> </label> <br><br> <label> <input class="checkbox" type="checkbox"><span class="values">Cherry</span> </label> </form> </div> <div id="output"></div> </body> </html> 

I realized the answer was to use .previousSibling and .nextSibling after posting the question, so I went ahead and finished all the code for the input/output part of the assignment. Then, I realized someone else mentioned .previousSibling in response to the first answer attempt. Thanks everyone!

window.onload = UncheckAll;
function UncheckAll() { 
    var w = document.getElementsByTagName('input'); 
    for (var i = 0; i < w.length; i++) { 
        if (w[i].type == 'checkbox') { 
            w[i].checked = false; 
        }
    }
}

document.getElementById("input").addEventListener("click", function(e){
    //Click Input Text - Box Checks and Output Text Appears
    if (e.target.nodeName === "SPAN") {
        if (e.target.previousSibling.checked === false) {
            var node = document.createElement("P");
            var textnode = document.createTextNode(e.target.innerHTML);

            node.appendChild(textnode);
            document.getElementById("output").appendChild(node);
            node.setAttribute("class", "outputItem")

            e.target.previousSibling.checked = true;
            return;
        }
    }

    //Click Input Text - Box Unchecks and Output Text Disappears
    if (e.target.nodeName === "SPAN") {
        if (e.target.previousSibling.checked === true) {
            for (i = 0; i < document.getElementsByClassName("outputItem").length; i++) {
                if (e.target.innerHTML === document.getElementsByClassName("outputItem")[i].innerHTML) {
                    document.getElementsByClassName("outputItem")[i].remove();
                    e.target.previousSibling.checked = false;
                    return;
                }
            }
        }
    }

    //Check Box - Output Text Appears
    if (e.target.type === "checkbox") {
        if (e.target.checked === true) {
            var node = document.createElement("P");
            var textnode = document.createTextNode(e.target.nextSibling.innerHTML);

            node.appendChild(textnode);
            document.getElementById("output").appendChild(node);
            node.setAttribute("class", "outputItem")
            return;
        }
    }

    //Uncheck Box - Output Text Disappears
    if (e.target.type === "checkbox") {
        if (e.target.checked === false) {
            for (i = 0; i < document.getElementsByClassName("outputItem").length; i++) {
                if (e.target.nextSibling.innerHTML === document.getElementsByClassName("outputItem")[i].innerHTML) {
                    document.getElementsByClassName("outputItem")[i].remove();
                    return;
                }
            }
        }

    }



});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM