the result is my list is getting bigger every time i call this function. i know i am very close to the solve my problem but im getting really nervous these days. Any help or comment will be appreciated.
function devicesoff() {
var devices = document.querySelectorAll("[id^='device_']");
for (var i = 0; i < devices.length; i++) {
if (devices[i].checked === false) {
var ele = devices[i].name;
var para = document.getElementById("p");
var list = document.createElement("li");
var t = document.createTextNode(ele + "\n");
list.appendChild(t);
para.appendChild(list);
}
}
First clear the old devices, then add new ones.
function devicesOff() { var devices = document.querySelectorAll("[id^='device_']"); var para = document.getElementById("p"); para.innerHTML = ""; // clear old devices first // add new ones for (var i = 0; i < devices.length; i++) { if (devices[i].checked === false) { var ele = devices[i].name; var list = document.createElement("li"); var t = document.createTextNode(ele + "\\n"); list.appendChild(t); para.appendChild(list); } } }
<input type="checkbox" id="device_1" name="device_1" /> <input type="checkbox" id="device_2" name="device_2" /> <input type="checkbox" id="device_3" name="device_3" /> <input type="button" value="devicesOff" onclick="devicesOff()" /> <ul id="p"> </ul>
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.