Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added js #3

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
$(function(){

//list
$list = $('ul');


// hide all elements of the list and fade them in
$('li').hide().each( function (i) {
$(this).delay(250 * i).fadeIn(1000);
});

//update item counter
function updateItemCounter(){
var itemCount = $('li').length;
$('#counter').text(itemCount);
}
updateItemCounter();

// hide button and show button
$("#newItemButton").show();
$('#newItemForm').hide();

$('#showForm').on('click', function () {
$("#newItemButton").hide();
$('#newItemForm').show();
})

// add new item
$('#add').on('click',function(e){
e.preventDefault();
var text = $('#itemDescription').val();
var $newItem = $("<li>" +text+ "</li>");
$newItem.on('click', function (){
var $li = $(this);
completeItem($li);
})
$list.append($newItem);
$('#itemDescription').val("");
updateItemCounter();

$("#newItemButton").show();
$('#newItemForm').hide();
})

//remove items on click
$('li').on('click',function(){
var $li = $(this);
completeItem($li);

})

function completeItem ( $li ) {
//var $li = $(this);
if($li.hasClass('complete')) {
$li.animate({
opacity: 0.0,
paddingLeft: '+= 180'
}, 500, 'swing', function() {
$li.remove();
updateItemCounter();
});
} else {
$li.addClass('complete');
$list.append($li).hide().fadeIn(200);
}
}

});