๐Ÿ™‚To Do List

An ji yoonยท2023๋…„ 6์›” 22์ผ
0

JS ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
3/3



โœ๏ธ๊ธฐ๋Šฅ๊ฐœ๋ฐœ


01. HTML ์ž‘์„ฑํ•˜๊ธฐ

<header class="head">
  <p class="title en">To Do List</p>
  <button class="clear en" onclick="clearDiv()">Clear</button>
</header>
<div class="inner">
  <div class="todo-wrap">
    
    <input type="text" id="todo" class="ipBox en" placeholder="New Memo" 
           onfocus="this.placeholder=''" onblur="this.placeholder='New Memo'">
    
    <button id="summit" class="ipBox en" onclick="addTodo()">Enter</button>
  </div>
  <ul id="list-box"></ul>
</div>
  • placeholder="" : input์— ์˜ˆ์‹œ ํ…์ŠคํŠธ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • onfocus="this.placeholder=''" : input์— ํฌ์ปค์Šค ๋ฌ์„๋•Œ this.placeholder='' ์ฆ‰ ๋นˆ ์นธ์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š”๊ฒƒ์ด๋‹ค.
  • onblur="this.placeholder='New Memo'" : input์— ํฌ์ปค์Šค๊ฐ€ ์•„์›ƒ๋ฌ์„๋•Œ ๋ณด์—ฌ์ฃผ๋Š” ํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


02-1 js์ž‘์„ฑํ•˜๊ธฐ


๋จผ์ € button์„ ํด๋ฆญํ–ˆ์„๋•Œ input์— ์ž…๋ ฅ๋œ๊ฐ’์„ ul์•ˆ์— html์š”์†Œ๋กœ ๋ฐ›์•„์™€ ์‚ฝ์ž…ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
// button onclick ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ 
function addTodo(){
      const todo = document.getElementById('todo');
      const listBox = document.getElementById('list-box');
  
      // input์ด ๊ณต๋ฐฑ์ผ๋•Œ ๊ฒฝ๊ณ ์ฐฝ ๋„์šฐ๊ธฐ
      if(todo.value == ''){ 
        alert('ํ•  ์ผ์„ ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”.');
      }
      // ๊ณต๋ฐฑ๋ž€์ด ์•„๋‹๋•Œ ์•„๋ž˜์˜ html ์š”์†Œ๋ฅผ listBox ๋’ค๋กœ ์‚ฝ์ž…
      else{ 
        let list = `<div id="appendDiv" class="div">
                    <div class="inputCover">
                    <input type="checkbox" id="checkBox" class="checked">
                    <p class="text">${todo.value}</p>
                    </div>
                    <button class="delete en">Delete</button>
                    </div>`;

        // list ์ถ”๊ฐ€
        listBox.insertAdjacentHTML('beforeend', list);
      }
};


02-2 js์ž‘์„ฑํ•˜๊ธฐ

ul์•ˆ์— ์‚ฝ์ž…๋œ html์š”์†Œ์—๋Š” checkbox ์™€ delete๋ฒ„ํŠผ์ด ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ทธ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
์—ฌ๊ธฐ์„œ ์ฃผ์˜ ํ•  ๊ฒƒ์€ ์ƒ์„ฑ๋˜๋Š” html์š”์†Œ == list ๊ฐ€ ํ•œ๊ฐœ๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— for ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ
์ƒ์„ฑ๋œ list์˜ ๊ธธ์ด๋งŒํผ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋ถ€์ฐฉ์‹œ์ผœ์ค˜์•ผ ๊ธฐ๋Šฅ์ด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

// ์ฒดํฌ๋ฐ•์Šค
let checked = document.querySelectorAll('.checked');

for(let i = 0; i < checked.length; i++){
  checked[i].addEventListener('change', (event) =>{

    let text = document.querySelectorAll('.text')[i];

    if(event.currentTarget.checked){
      text.style.textDecoration = 'line-through';
      text.style.color = '#7c7c7c';
    }
    else{
      text.style.textDecoration = 'none';
      text.style.color = '#000'
    }
  });
};

// ์‚ญ์ œ๊ธฐ๋Šฅ
let del = document.querySelectorAll('.delete');
for(let i = 0; i<del.length; i++){
  del[i].addEventListener('click', listDel);
}
todo.value = null; //input๋‚ด์šฉ ๋ฆฌ์…‹ํ•˜๊ธฐ
};

// ์‚ญ์ œ๋ฒ„ํŠผ ๊ธฐ๋Šฅ
function listDel(e){
      //ํด๋ฆญํ•œ ์š”์†Œ์˜ ๋ถ€๋ชจ์š”์†Œ๋ฅผ ์ฐพ์•„ ์‚ญ์ œ
      let target = e.target.parentNode; 
      target.remove();
};


02-3 js์ž‘์„ฑํ•˜๊ธฐ


์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ์„ ๊ธฐ๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋„ ์ž‘์„ฑํ•ด์ค€๋‹ค. ul
// clear ๋ฒ„ํŠผ ๊ธฐ๋Šฅ
function clearDiv(){
    let a = document.getElementById('list-box');
    if(a !== null){
      a.innerHTML = '';
    }
};
profile
์‹ค์Œ์—์„œ ํ”„์—”๊นŒ์ง€

0๊ฐœ์˜ ๋Œ“๊ธ€