Skip to content

Instantly share code, notes, and snippets.

@mona87
Last active August 29, 2015 14:21
Show Gist options
  • Save mona87/505575f22ee775aa2d40 to your computer and use it in GitHub Desktop.
Save mona87/505575f22ee775aa2d40 to your computer and use it in GitHub Desktop.
Todo List #2
$(document).on('ready', start);
function start(event){
var $inputBox = $('#inputBox');
var $btn = $('#btn');
var $sect = $('#sect');
var $form = $('#myForm');
var arraylist = [];
var list = []
var obj = {};
var count = 0;
$form.on('submit', render);
function render(e){
count++;
e.preventDefault();
obj = {};
obj.id = count;
obj.todo = obj.id+') '+$inputBox.val();
obj.completed = false;
obj.deleted = false;
arraylist.push(obj);
$inputBox.val('');
for(var i = 0; i< arraylist.length; i++){
list.push('<div id='+ arraylist[i].id+'>'+arraylist[i].todo+'</div>');
}
$sect.html('');
$sect.html(list);
check();
$sect.on('click','div', strikeThru);
list = [];
}
//checks the arraylist for completed values that render a strikethrough
function check(){
for(var i = 0; i< arraylist.length; i++){
if(arraylist[i].completed === true){
//console.log(list[i]);
console.log($sect.find('#'+arraylist[i].id))
var $div = $($sect.find('#'+arraylist[i].id));
$div.css('text-decoration', 'line-through');
}
}
}
function strikeThru(e){
var $this = $(this);
e.preventDefault();
for(var i = 0; i < arraylist.length; i++){
if($this.html() === arraylist[i].todo ){
//console.log($this.html());
if(arraylist[i].completed === false){
//set completed to true and scrathes it off
arraylist[i].completed = true;
arraylist[i].deleted = true;
$this.css('text-decoration', 'line-through');
console.log(arraylist[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment