I have input
that after filling in and pressing Enter , it creates a list with checkbox
's. The problem is that when it is populated again, it edits the list item already created and I want to create a new item, do not edit the previous one.
I want to make the list populated with input
, create multiple li
's and not just one and keep editing / overwriting it. How can I do this?
// verifica se a tecla apertada é o enter
function OnEnter(evt) {
var key_code = evt.keyCode ? evt.keyCode :
evt.charCode ? evt.charCode :
evt.which ? evt.which : void 0;
if (key_code == 13) {
return true;
}
}
var j = 1;
function recarrega(e) {
if(OnEnter(e))
{
var p = document.getElementById('Foo');
var filhos = p.childNodes;
for (i = filhos.length - 1; i >= 0; i--) {
if (filhos[i].tagName == 'LI') {
p.removeChild(filhos[i]);
}
}
var tarefa = document.getElementById("my_span").value;
document.getElementById("my_span").value = "";
var li = document.createElement('li');
p.appendChild(li);
li.id = 'my_span'+ j;
li.innerHTML = "<input type='checkbox' id='ckb" + j + "' value='" + j + "' onclick='my_fun(" + j + ");'>" + tarefa;
j++;
return false;
}
else
{
return true;
}
}
// quando marco o checkbox ele risca a string da li
function my_fun(j) {
var chkbox = "ckb" + j;
var my_span = "my_span" + j;
var msg = chkbox + " " + my_span;
if ($("#ckb"+ j).is(':checked')) {
document.getElementById(my_span).style.textDecoration = 'line-through';
} else {
document.getElementById(my_span).style.textDecoration = 'none';
}
}
<form>
<label for="ctarefa">Tarefas:</label>
<input onkeypress="return recarrega(event);" type="text" id='my_span'/><br />
<div class="boxLista">
<ul id="Foo">
</ul>
</div>
</form>