I’m trying to make a to do list that you can check off completed task and it would calculate the percentage done. So the first thing I tried was making a couple of checkboxes and adding an event listener for the click then I’ll check the value probably to see if it is = true or something like that.
I’m trying to figure out a way to define var i if I click on the first checkbox I want it to be set to 0 and the second set to 1. But I want it to dynamically set the var i because the user will be adding new checkboxes and I can’t declare what i equals after they add a new item. Anyway this is what I have so far.
<!DOCTYPE html>
<html>
<head>
<title>To Do List</title>
<script type="text/javascript">
var list;
var checkboxes;
document.addEventListener("DOMContentLoaded",load);
function load(){
list = get("list");
checkboxes = getTag("input");
checkboxes[i].addEventListener("click",toggle);
}
function toggle(){
alert(this.value);
}
function get(id){
return document.getElementById(id);
}
function getTag(tag){
return document.getElementsByTagName(tag);
}
</script>
</head>
<body>
<h1>Kung Fu To Do List 1.0</h1>
<ul id="list">
<li><input type="checkbox" value="true"></li>
<li><input type="checkbox" value="false"></li>
</ul>
</body>
</html>
Honestly, I’m not sure what you mean by defining
i. But it seems like what you’re looking for is calculating the percentage complete, and keeping that updated when/if the user adds new items. I wouldn’t keep track ofcheckboxesbut instead loop over them when you need to calculate. Further, having a method that adds new checkboxes for you can then hook in the events you want to monitor. Something like: (jsfiddle: http://jsfiddle.net/nuuyx/)