Snippets
Revised by
Glauco Pater
05fd16e
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sorrt</title>
<meta name="description" content="Add, Sort and Save result">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<style>
/*style.css*/
body {
text-align: center;
background-color: #3c6dc5;
color: white;
font-family: helvetica;
}
h1 {
margin-top: 100px;
}
ul {
display: inline-block;
cursor: move;
text-align: left;
width: 300px;
max-width: 100%;
}
li {
color: lightgray;
}
li:first-child {
font-size: 130%;
color: white;
}
input[type='submit'] {
background-color: red;
border: 0;
color: white;
padding: 5px 10px;
cursor: pointer;
}
input[type='button'] {
background-color: orange;
border: 0;
color: white;
padding: 5px 10px;
cursor: pointer;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>
<script>
/*main.js*/
$(document).ready(function($) {
addNewValue = function(newTask) {
var newLi = $('<li>' + newTask + '</li>');
newLi.on('click', function() {
$(this).remove(); // Attach the event handler *before* adding the element
});
$('ul').addClass('listitems').prepend(newLi); // To put the new task at the top of the list
$('.input').val('');
}
$('form').submit(function() {
var newVal = $('.input').val();
if (newVal !== '') {
if (!isNaN(parseFloat(newVal))) {
addNewValue(newVal);
}
return false; // So the change persists
}
});
$('ul').sortable(); // Because what good is a to-do list that you can't sort? :)
$('input:button').click(function() {
sorrt();
});
function sort_li(a, b) {
return ($(b).text() < $(a).text()) ? 1 : -1;
}
function sorrt() {
var orderedlist =
$("ul.listitems li").sort(function(a, b) {
return $(b).text() - $(a).text()
});
orderedlist.appendTo('.listitems');
var orderedListValues = [];
$(orderedlist).each(function() {
orderedListValues.push($(this).text());
});
window.localStorage.setItem("sorrt", orderedListValues);
}
function getStorage() {
return window.localStorage.getItem("sorrt");
}
function main() {
var storage = getStorage();
console.log(storage);
if (storage != null && storage != '') {
$(storage.split(",")).each(function(index, val) {
console.log(index.val);
addNewValue(val);
});
}
}
});
</script>
<body>
<h1>
SORRT
</h1>
<form>
<label>Enter a new value:</label>
<input class="input" type="text">
<input type="submit" value="Add">
<input type="button" value="Sort">
</form>
<ul title="Click to delete; drag to reorder">
</ul>
</body>
|
You can clone a snippet to your computer for local editing. Learn more.