onKeyUp event (not work)?

405 views Asked by At

I want that when I do mouseup i can to have the option of delete or no the list with "window.confirm". The console say "Uncaught ReferenceError: li is not defined".

var add =  document.getElementById('create-memo');

function newMemo(list, itemText){
    var listMemo = document.createElement('li');
    listMemo.className = 'memo';
    listMemo.innerText = itemText;
    list.appendChild(listMemo);

  listMemo.addEventListener('mouseup', removeList);
}

function removeList(){
    if (window.confirm('You want remove this memo?')) {
        li.parentNode.removeChild(listMemo);
    }
}
2

There are 2 answers

3
dsuess On BEST ANSWER

UPDATED

Check out this running demo. I hope it's what you are looking for.

// store element reference in variable, e.g. if list has this id
var items = document.querySelectorAll('#listMemo li');

// assign event listener to each
console.dir(items);
var i;
for (i = 0; i < items.length; i++) {
  items[i].addEventListener('click', removeListItem);
  console.log('add EL to ' + i);
}

function removeListItem(mouseEvent) {
  // get element 
  //debugger;
  var element = mouseEvent.target;
  if (window.confirm('Do you want to delete "' + element.textContent + '" ?')) {
    // remove the node
    element.parentNode.removeChild(element);
  };
}
<ul id="listMemo">
  <li>first item</li>
  <li>second item</li>
  <li>third item</li>
  <li>fourth item</li>
</ul>

2
Sam Hanley On

The argument you want to provide to .addEventListener for a keyup event is keyup, rather than onKeyUp. Changing that should resolve your issue.