<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>댓글 수정 기능</title>
    <style>
        .comment-box {
            border: 1px solid #ddd;
            padding: 10px;
            margin: 10px 0;
        }
        .edit-area {
            display: none; /* 처음에는 안 보이도록 설정 */
            margin-top: 10px;
        }
    </style>
</head>
<body>

    <div id="comments">
        <div class="comment-box" data-id="1">
            <p class="comment-text">이것은 첫 번째 댓글입니다.</p>
            <button class="edit-btn">수정</button>
            <div class="edit-area">
                <textarea class="edit-input" rows="3" cols="50"></textarea>
                <button class="save-btn">저장</button>
            </div>
        </div>

        <div class="comment-box" data-id="2">
            <p class="comment-text">이것은 두 번째 댓글입니다.</p>
            <button class="edit-btn">수정</button>
            <div class="edit-area">
                <textarea class="edit-input" rows="3" cols="50"></textarea>
                <button class="save-btn">저장</button>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener("DOMContentLoaded", function() {
            // 모든 "수정" 버튼을 가져옴
            const editButtons = document.querySelectorAll(".edit-btn");

            editButtons.forEach(button => {
                button.addEventListener("click", function() {
                    const commentBox = this.closest(".comment-box"); // 해당 댓글 박스 찾기
                    const commentText = commentBox.querySelector(".comment-text").innerText; // 기존 댓글 내용 가져오기
                    const editArea = commentBox.querySelector(".edit-area"); // textarea 영역 찾기
                    const editInput = commentBox.querySelector(".edit-input"); // textarea 찾기

                    editInput.value = commentText; // textarea에 기존 댓글 내용 넣기
                    editArea.style.display = "block"; // textarea 보이게 하기
                });
            });

            // 모든 "저장" 버튼에 이벤트 추가
            const saveButtons = document.querySelectorAll(".save-btn");

            saveButtons.forEach(button => {
                button.addEventListener("click", function() {
                    const commentBox = this.closest(".comment-box");
                    const editInput = commentBox.querySelector(".edit-input");
                    const commentText = commentBox.querySelector(".comment-text");

                    commentText.innerText = editInput.value; // 댓글 내용 업데이트
                    commentBox.querySelector(".edit-area").style.display = "none"; // textarea 숨기기
                });
            });
        });
    </script>

</body>
</html>

Total
Today
Yesterday