๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’› Frontend/๐Ÿ’› Frontend : JavaScript

[JavaScript] ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ํ™”๋ฉด์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•˜๊ธฐ (ํˆฌ๋‘๋ฆฌ์ŠคํŠธ)

by eyes from es 2025. 2. 3.
728x90
๋ฐ˜์‘ํ˜•

โœ… ๋ชฉํ‘œ


  • ํ…์ŠคํŠธ ํ•„๋“œ ๊ฐ’์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜ li์— ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค.
  • ์ถ”๊ฐ€๋œ li๋ฅผ ํด๋ฆญํ•˜๋ฉด '์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?' confrim ์ฐฝ์ด ๋œจ๊ณ  'ํ™•์ธ'์„ ๋ˆ„๋ฅด๋ฉด ์‚ญ์ œ๋œ๋‹ค.

#appendChild   #createTextNode    # createElement    #์ด๋ฒคํŠธ์œ„์ž„

 

 

 

 

โœ… Html


<body>
  <h1>Web Programming</h1>
  <p>๊ณต๋ถ€ํ•  ์ฃผ์ œ๋ฅผ ๊ธฐ๋กํ•ด ๋ณด์„ธ์š”</p>

      <label for="input">์ฃผ์ œ</label>
      <input id="input" type="text" autofocus>
      <button class="button">์ถ”๊ฐ€</button>

    <hr>

    <ul class="itemList">
      <li>์ถ”๊ฐ€ํ• ๊ฒŒ์š”</li>
    </ul>
</body>

 

label์˜ for๊ณผ input์˜ id๋ฅผ ๋งž์ถฐ์ค๋‹ˆ๋‹ค.

 

label์˜ for ์†์„ฑ์€ ํ•ด๋‹น ๋ ˆ์ด๋ธ”์ด ์–ด๋–ค ์ž…๋ ฅ ์š”์†Œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž๊ฐ€ label์„ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ์ž…๋ ฅ ํ•„๋“œ์— ํฌ์ปค์Šค๊ฐ€ ๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

label์ธ ์ฃผ์ œ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ž…๋ ฅ ํ•„๋“œ์— ํฌ์ปค์Šค(์ปค์„œ) ๊ฐ€ ์ƒ๊น€

 

๋•Œ๋ฌธ์— ํŠนํžˆ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ์—์„œ ์ž…๋ ฅ ํ•„๋“œ๋ณด๋‹ค ๋ ˆ์ด๋ธ”์„ ํด๋ฆญํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๊ธฐ ๋” ํŽธ๋ฆฌํ•ด์ง‘๋‹ˆ๋‹ค.

label์— for์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ž…๋ ฅ ์š”์†Œ์˜ id์™€ ๋™์ผํ•˜๊ฒŒ ๋งž์ถฐ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 


 

โœ… JavaScript - ํ…์ŠคํŠธ ์ž…๋ ฅ๊ฐ’์„ li์— ์ถ”๊ฐ€

<script>
    const button = document.querySelector('.button');
    const input = document.querySelector('#input');
    const itemList = document.querySelector(".itemList");

    document.addEventListener('DOMContentLoaded', function(){
        
      button.addEventListener('click', function(e){
          e.preventDefault();
          
          var msg = input.value;
          if(msg !== ""){
            newRegister(msg);
            
            // ์ž…๋ ฅ ํ›„ ํ…์ŠคํŠธ ํ•„๋“œ ๊ฐ’์„ ๋น„์›Œ์คŒ
            input.value = "";
          }
        });

    });

    function newRegister(msg){
      // ์š”์†Œ๋ฅผ ๋งŒ๋“ค๊ณ 
      const newItem = document.createElement('li');
      // ์š”์†Œ์— text ์ถ”๊ฐ€
      const newText= document.createTextNode(msg);

      // ์š”์†Œ์— ํ…์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ 
      newItem.appendChild(newText);

      // ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ์™„์„ฑ๋œ li๋ฅผ ๋„ฃ์–ด์ค€๋‹ค
      itemList.appendChild(newItem);
    }
    
  </script>

 

  • ์—”ํ„ฐ ํ˜น์€ '์ถ”๊ฐ€' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด
  • ํ…์ŠคํŠธ ํ•„๋“œ์— ์ž…๋ ฅ ๋ฐ›์€ ๊ฐ’์„ ์•„๋ž˜ li์˜ ๋งจ ์•„๋ž˜๋ถ€ํ„ฐ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค

 


 

์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

 

1. ์š”์†Œ๋ฅผ ๋งŒ๋“ค๊ณ 

์ƒˆ๋กœ์šด 'li'๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด createElement ํ•ฉ๋‹ˆ๋‹ค.

const newItem = document.createElement('li');

 

2. text๋ฅผ ๋งŒ๋“ค๊ณ 

์ƒˆ๋กœ์šด TextNode๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด msg์˜ ๊ฐ’์œผ๋กœ textNode๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

const newText= document.createTextNode(msg);

 

3. ์š”์†Œ์— text๋ฅผ ๋„ฃ๊ณ 

์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  'li'์— msg ๊ฐ’์ธ textNode๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

newItem.appendChild(newText);

 

4. ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค.

์ตœ์ƒ๋‹จ ul (์ „์ฒด ๋ฆฌ์ŠคํŠธ) ์— ์™„์„ฑ๋œ li๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

itemList.appendChild(newItem);

 

 


โœ… JavaScript - li๋ฅผ ํด๋ฆญํ•ด์„œ ์‚ญ์ œํ•˜๊ธฐ

confirm ์ฐฝ์—์„œ ํ™•์ธ์„ ๋ˆ„๋ฅด๋ฉด ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.

 

์ด์ œ ์ถ”๊ฐ€๋œ li๋ฅผ ํด๋ฆญํ•˜๋ฉด ์‚ญ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ๊ฐ์˜ li์— ๋ชจ๋‘ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋‹ฌ์•„์•ผ ํ• ๊นŒ์š”?

 

 

li๊ฐ€ ์•„์ฃผ ๋งŽ์•„์ง„๋‹ค๋ฉด ์–ด๋ ต์Šต๋‹ˆ๋‹ค.

 

 

์ด๋Ÿด ๋•Œ '์ด๋ฒคํŠธ ์œ„์ž„'์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ์ด๋ฒคํŠธ ์œ„์ž„ ๐Ÿ’ก

์ด๋ฒคํŠธ ์œ„์ž„์ด๋ž€ ๋ถ€๋ชจ ์š”์†Œ์— ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ•˜๋‚˜๋งŒ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ชจ๋“  ์ž์‹ ์š”์†Œ์˜ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋Œ€์ƒ์ด event ๊ฐ์ฒด์˜ target ํ”„๋กœํผํ‹ฐ (event.target)์ธ์ง€ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

itemList.addEventListener('click', (e) => {
          if(e.target.tagName === 'LI'){
            if(confirm("์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?")){
              e.target.remove();
            };
          }
        });

 

  • ์ „์ฒด ๋ฆฌ์ŠคํŠธ์ธ ul itemList๋ฅผ ํด๋ฆญํ•œ ํ›„
  • ํด๋ฆญํ•œ ํƒ€๊ฒŸ์˜ tagName์ด 'LI'์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ํƒœ๊ทธ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

์ดํ›„, confrim ์ฐฝ์—์„œ ๋ฐ˜ํ™˜ ๊ฐ’์ด true ์ฆ‰, ํ™•์ธ์„ ๋ˆŒ๋ €๋‹ค๋ฉด ํ•ด๋‹น target์„ ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 


โœ… ์ „์ฒด ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>์ž…๋ ฅ๊ฐ’ ๋ฐ‘์— ์ถ”๊ฐ€ํ•˜๊ธฐ</title>

  <style>
    input{
      border-radius: 5px;
    }

    button{
      border-radius: 5px;
      font-size: 1.1em;
    }
  </style>

</head>
<body>
  <h1>Web Programming</h1>
  <p>๊ณต๋ถ€ํ•  ์ฃผ์ œ๋ฅผ ๊ธฐ๋กํ•ด ๋ณด์„ธ์š”</p>

      <label for="input">์ฃผ์ œ</label>
      <input id="input" type="text" autofocus>
      <button class="button">์ถ”๊ฐ€</button>

    <hr>

    <ul class="itemList">
      <li>์ถ”๊ฐ€ํ• ๊ฒŒ์š”</li>
    </ul>


  <script>
    const button = document.querySelector('.button');
    const input = document.querySelector('#input');
    const itemList = document.querySelector(".itemList");

    document.addEventListener('DOMContentLoaded', function(){
        
      button.addEventListener('click', function(e){
          e.preventDefault();
          
          var msg = input.value;
          if(msg !== ""){
            newRegister(msg);
            input.value = "";
          }
        });

        itemList.addEventListener('click', (e) => {
          if(e.target.tagName === 'LI'){
            if(confirm("์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?")){
              e.target.remove();
            };
          }
        });


    });

    function newRegister(msg){
      // ์š”์†Œ๋ฅผ ๋งŒ๋“ค๊ณ 
      const newItem = document.createElement('li');
      // ์š”์†Œ์— text ์ถ”๊ฐ€
      const newText= document.createTextNode(msg);

      // ์š”์†Œ์— ํ…์ŠคํŠธ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ 
      newItem.appendChild(newText);

      // ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ์™„์„ฑ๋œ li๋ฅผ ๋„ฃ์–ด์ค€๋‹ค
      itemList.appendChild(newItem);
    }
    
  </script>
</body>
</html>
728x90
๋ฐ˜์‘ํ˜•