Update the value of a user based on the click of a checkbox In an admin view on an intranet I maintain, there is a table of users present that allows admins to validate or delete users. Right now, the way the user is validated (or how they have their validation revoked) is by clicking a checkbox (i.e. a check in the checkbox signifies the user is validated, but no check means the user is not validated).

And right now I have an if/else statement that looks to see if the box has just been checked (or unchecked) and then sends the data to the server to achieve the desired outcome.

The code is not DRY at all, because the only difference in the code from the if/else statement is the state of the checkbox. How can I make the following code more DRY?

const chkBox = document.querySelectorAll("input[type=checkbox]");

for (box of chkBox) {
    box.addEventListener("change", updateUser, false);
}

function updateUser() {

    const messageBox = document.getElementById("message");

    if (this.checked) {

    // Getting the values to post to the server and the initialization requirements
        const postingValues = JSON.stringify({id:this.value, validated:true});
        const fetchInit = {method: "POST", headers: {"Content-type": "application/json"}, body: postingValues};

    // Making the fetch call and returning the message to the user
        fetch("index.php?action=updateLogin", fetchInit).then(function(response) {
            response.text().then(function(value) {
                messageBox.innerHTML = value;
            });
        });
    } else {

    // Getting the values to post to the server and the initialization requirements
        const postingValues = JSON.stringify({id:this.value, validated:false});
        const fetchInit = {method: "POST", headers: {"Content-type": "application/json"}, body: postingValues};

    // Making the fetch call and returning the message to the user
        fetch("index.php?action=updateLogin", fetchInit).then(function(response) {
            response.text().then(function(value) {
                messageBox.innerHTML = value;
            });
        });
    }
}
So chalk this one up to me not giving it enough thought (although I've had this in the back of my mind for about two weeks and today just decided to use my Saturday to actually sit down and refactor the entire application):
The answer is that I put the state of the checkbox in a variable and I don't need to check for it at all, I can just pass it through into my postingValues object. The code now looks like this:

const chkBox = document.querySelectorAll("input[type=checkbox]");

for (box of chkBox) {
    box.addEventListener("change", updateUser, false);
}

function updateUser() {

// Getting the state of the checkbox and the DOM element to display the message
    const messageBox = document.getElementById("message");
    const validated = this.checked;

// Getting the values to post to the server and the initialization requirements
    const postingValues = JSON.stringify({id:this.value, validated:validated});
    const fetchInit = {method: "POST", headers: {"Content-type": "application/json"}, body: postingValues};

// Making the fetch call and returning the message to the user
    fetch("index.php?action=updateLogin", fetchInit).then(function(response) {
        response.text().then(function(value) {
            messageBox.innerHTML = value;
        });
    });
}