728x90
300x250
SMALL
jQuery
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<ul class="dynamic-list"></ul>
<button class="add">추가</button>
<script>
$('.add').click(function(){
$('.dynamic-list').append('<li>새 항목</li>');
});
</script>
Vanilla JS
<ul class="dynamic-list"></ul>
<button class="add">추가</button>
<script>
const list = document.querySelector('.dynamic-list');
document.querySelector('.add').addEventListener('click',function(){
const li = document.createElement("li");
li.textContent = "새 항목";
list.appendChild(li);
})
</script>
- const li = document.createElement("li"); //새 li요소를 생성
- li.textContent = "새 항목"; //텍스트 내용을 설정
- list.appendChild(li); //ul 또는 ol에 추가
728x90
반응형
LIST
'javaScript > Vanilla JS' 카테고리의 다른 글
Vanilla JS 간단한 모달창 열기/닫기 (0) | 2025.04.22 |
---|---|
Vanilla JS 반복문, 동적 요소 제어 (0) | 2025.04.20 |
Vanilla JS 입력창에 입력한 값 가져오기 (0) | 2025.04.19 |
클래스 제어, 토글, 조건문 (0) | 2025.04.18 |
Vanilla JS 기초 연습 - 버튼 클릭하면 alert 띄우기 (0) | 2025.04.17 |
댓글