SHARE
TWEET

Untitled

a guest Feb 16th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var elementList = document.querySelector('#app ul');
  2.  
  3. var inputElement = document.querySelector('#app input');
  4.  
  5. var buttonElement = document.querySelector('#app button');
  6.  
  7. var todos = [
  8.   'Estudar JS',
  9.   'Ler you dont know js',
  10.   'Participar da comunidade rocketseat'
  11. ]
  12.  
  13. const removeElement = index => todos.splice(index, 1);
  14.  
  15. const clearElementList = () => elementList.innerHTML = '';
  16.  
  17. const render = elements => el => {
  18.   var li = document.createElement('li');
  19.   var textLi = document.createTextNode(el);
  20.  
  21.   li.appendChild(textLi);
  22.  
  23.   var linkElement = document.createElement('a');
  24.   var textLink = document.createTextNode('[x]');
  25.  
  26.   linkElement.setAttribute('href', '#');
  27.  
  28.   linkElement.appendChild(textLink);
  29.   linkElement.onclick = () => {
  30.     var index = todos.indexOf(el);
  31.     console.log(el);
  32.     removeElement(index);
  33.     clearElementList();
  34.     renderElements(todos);
  35.   }
  36.  
  37.   li.appendChild(linkElement);
  38.  
  39.   elements.appendChild(li);
  40. }
  41.  
  42. const renderElement = render(elementList);
  43.  
  44. const renderElements = elements => {
  45.   const render = _ => {
  46.     renderElement(_);
  47.   }
  48.  
  49.   elements.forEach(render);
  50. }
  51.  
  52. renderElements(todos);
  53.  
  54. const clearInput = input => input.value = '';
  55.  
  56. const addTodo = input => {
  57.   return function () {
  58.     var value = input.value;
  59.     if(!value) return;
  60.  
  61.     todos.push(value);
  62.     clearElementList();    
  63.  
  64.     renderElements(todos);
  65.     clearInput(input);
  66.   }
  67. }
  68.  
  69. buttonElement.onclick = addTodo(inputElement);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top