Last active
August 29, 2015 14:01
-
-
Save StephenFiser/7c652614818d58ea81fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(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