vendredi 27 mars 2015

Javascript check/uncheck all checkboxes and write values to textarea

This is my first js script so be gentle with me :)


The problem is when I click on check all button, all checkboxes are checked but it won't write values to textarea, if I click individual checkboxes then the value is added/removed and that is ok, I'm just stuck on that check all/uncheck all button.


http://ift.tt/1HT4uQr





function check(chk) {
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}

function uncheck(chk) {
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}

var itemsAdded = Array();

function movetext(text) {
var i = itemsAdded.indexOf(text)
if ( i >= 0) {
itemsAdded.splice(i,1);
}
else {
itemsAdded.push(text);
}
document.getElementById("result").value=itemsAdded.join("\n");
}



<form>
<input type='checkbox' value='aaa' name="add" onclick='movetext(this.value)'/>a
<input type='checkbox' value='bbb' name="add" onclick='movetext(this.value)'/>b
<input type='checkbox' value='ccc' name="add" onclick='movetext(this.value)'/>c
<input type='checkbox' value='ddd' name="add" onclick='movetext(this.value)'/>d
<input type='checkbox' value='eee' name="add" onclick='movetext(this.value)'/>e

<input type="button" value="check all" onClick="check(this.form.add)">
<input type="button" value="uncheck all" onClick="uncheck(this.form.add)">

<br>

<textarea id="result" rows="8" cols="40"></textarea>






Aucun commentaire:

Enregistrer un commentaire