vendredi 1 septembre 2017

How to append multiple checkbox values into textarea with a click of one checkbox

hi guys need your help again. I have a javascript function which pass checkbox values into a textarea 'recipients', it works fine on check/uncheck and pass values accordingly into a textarea. What i want is to have one checkbox to check all checkbox and append values into a textarea.

Below is my javascript to pass vales into textarea 'recipients':

var textbox = document.getElementsByName("recipients")[0];
var checkboxes = document.getElementsByName("email");
for (var i = 0; i < checkboxes.length; i++) {
    var checkbox = checkboxes[i];
    checkbox.onclick = (function(chk){
        return function() {
            var value = "";
            for (var j = 0; j < checkboxes.length; j++) {
                if (checkboxes[j].checked) {
                    if (value === "") {
                        value += checkboxes[j].value;
                    } else {
                        value += ", " + checkboxes[j].value;
                    }
                }
            }
            textbox.value = value;
        }
    })(checkbox);
}

Please help.




Aucun commentaire:

Enregistrer un commentaire