Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

나 개발자 할래요

To Do List... 본문

개발자 되는 법.../TIL...

To Do List...

개발_자 2024. 7. 19. 13:05

2024.07.18 - [분류 전체보기] - `cloneNode`메서드

 

`cloneNode`메서드

`cloneNode`JS에서 DOM 노드를 복제하는 메서드복제본은 DOM 트리의 다른 위치에 삽입 가능하며,원래 노드의 구조와 속성 그대로 유지함 두 가지 모드 지원 : 얕은 복사(shallow clone), 깊은 복사(deep clone

onetwothreechachacha.tistory.com

<div id="bottom" class="bottom"></div>
<template id="template">
 <div class="list">
  <div class="todo">
   <div class="content">
    <label class="custom-checkbox">
     <input type="checkbox">
     <span class="checkmark"></span>
     <div class="info">
      <p class="text-date"></p>
     </div>
    </label>
   </div>
   <button class="delete">
    <i class="fa-solid fa-minus"></i>
   </button>
  </div>
 </div>
</template>

 

document.addEventListener("DOMContentLoaded", function () {
    const form = document.getElementById('form');
    const date = document.getElementById('date');
    const text = document.getElementById('text');
    const bottom = document.getElementById('bottom');
    const template = document.getElementById('template');

    let cloneCount = 0;

    form.addEventListener("submit", function (event) {
        event.preventDefault();

        const textValue = text.value.trim();
        const dateValue = date.value;

        if (dateValue !== "" && textValue !== "" && cloneCount < 4) {
            const clone = template.content.cloneNode(true); // 깊은 복사
            const textDateElement = clone.querySelector('.text-date');

            textDateElement.textContent = `${textValue} (${dateValue})`;

            const checkbox = clone.querySelector('input[type="checkbox"]');
            checkbox.addEventListener('change', function() {
                if (checkbox.checked) {
                    textDateElement.classList.add('strikethrough');
                } else {
                    textDateElement.classList.remove('strikethrough');
                }
            });

            const deletebtn = clone.querySelector('.delete');
            deletebtn.addEventListener("click", function () {
                deletebtn.parentNode.parentNode.remove();
                cloneCount--;
            });

            bottom.appendChild(clone);
            cloneCount++;
            text.value = "";
            date.value = "";
        }
    });
});

 

 

 

 

 


2024.07.18 - [분류 전체보기] - `appendChild`메서드

 

`appendChild`메서드

`appendChild`JS엣 DOM 요소를 다른 요소의 자식으로 추가할 때 사용이 메서드는 한 요소를 다른 요소의 마지막 자식으로 삽입함 기본 사용법parentElement.appendChild(childElement);`parentElement` : 자식 요소를

onetwothreechachacha.tistory.com

bottom.appendChild(clone);

`bottom`은 `form`제출 이벤트가 발생할 때 새로운 클론 요소를 추가하는 부모 요소

 

const clone = template.content.cloneNode(true);

클론 생성

클론은 `template`요소의 콘텐츠를 복제하여 생성됨

 

const textDateElement = clone.querySelector('.text-date');
textDateElement.textContent = `${textValue} (${textValue})`;

클론 수정

클론 내에 `.text-date`클래스를 가진 요소를 찾아서 사용자 입력 값 설정

 

bottom.appendChild(clone);

클론 추가

수정된 클론 요소를 `bottom` 요소의 마지막 자식으로추가

 

 

수정하면서 계속 업로드하겠습니다

'개발자 되는 법... > TIL...' 카테고리의 다른 글

JS 특강 완강을 하며..  (0) 2024.07.24
JS 특강 3주차를 들으며..  (1) 2024.07.23
To Do List 만들기  (0) 2024.07.09
To Do List 만들기  (0) 2024.06.28
개발 아티클 스터디  (0) 2024.06.21