Created
May 13, 2015 08:05
-
-
Save lduboeuf/823269a60f68278d1538 to your computer and use it in GitHub Desktop.
todo avec jquery
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
<!doctype html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9" lang=""> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js" lang=""> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<title></title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="apple-touch-icon" href="apple-touch-icon.png"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> | |
<script type="text/javascript"> | |
var todolist = { | |
"todos": [ | |
{ "name" : "todo 1"}, | |
{ "name" : "todo 2"}, | |
{ "name" : "todo 3"} | |
] | |
} | |
//fonction qui ajoute un todo a la liste ul | |
function add_todo(todo){ | |
var td = $('<li></li>').html(todo); | |
var td_delete = $('<input />', { type: 'checkbox' }) | |
td_delete.click(function() { | |
if (this.checked) { | |
$(this).parent().css('textDecoration', 'line-through'); | |
} else { | |
$(this).parent().css('textDecoration', 'none'); | |
} | |
}); | |
td.append(td_delete); | |
$( "#todos-list" ).append( td); | |
} | |
//fonction appellée une fois le document html complètement chargé | |
$( document ).ready(function() { | |
//initialisation | |
//parcours de la liste des todos et création des <li> | |
$.each(todolist.todos, function(i, todo) { | |
//use obj.id and obj.name here, for example: | |
add_todo(todo.name); | |
}); | |
//le click surle bouton ok ajoute un todo | |
$('#btn-create-todo').click(function(){ | |
var td = $('#todo').val(); | |
if (td != ''){ | |
add_todo(td); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<!--[if lt IE 8]> | |
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> | |
<![endif]--> | |
<h1>TODO list</h1> | |
<div id="form"> | |
<input type="text" id="todo"/> | |
<input type="button" id="btn-create-todo" value="ok"/> | |
</div> | |
<ul id="todos-list"> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment