Skip to content

Instantly share code, notes, and snippets.

@StephenFiser
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save StephenFiser/7c652614818d58ea81fb to your computer and use it in GitHub Desktop.

Select an option

Save StephenFiser/7c652614818d58ea81fb to your computer and use it in GitHub Desktop.
$(document).ready(function() {
listenForChecks();
// Event Listeners
$('#new-to-do-item').keyup(function(e) {
if (e.which === 13) {
var text = $(this).val();
var listItem = "<li><input type='checkbox'>" + text + "</li>"
$('ul').append(listItem);
listenForChecks();
}
});
$('#sort').click(function(e) {
e.preventDefault();
sortToDos();
listenForChecks();
});
$('#reverse-sort').click(function(e) {
e.preventDefault();
reverseToDos();
listenForChecks();
});
// Custom Functions
function listenForChecks() {
$('input[type="checkbox"]').click(function() {
console.log(this);
console.log($(this));
if (this.checked) {
$(this).parent().css("background-color", "#e8e8e8");
} else {
$(this).parent().css("background-color", "#ffffff");
}
});
}
function compare(a,b) {
if ($(a).text() < $(b).text())
return -1;
if ($(a).text() > $(b).text())
return 1;
return 0;
}
function reverseCompare(a,b) {
if ($(a).text() < $(b).text())
return 1;
if ($(a).text() > $(b).text())
return -1;
return 0;
}
function sortToDos() {
var listElements = $('li');
listElements.sort(compare);
$('li').remove();
$('ul').append(listElements);
}
function reverseToDos() {
var listElements = $('li');
listElements.sort(reverseCompare);
$('li').remove();
$('ul').append(listElements);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment