728x90
300x250
SMALL
다음의 jQuery를 Vanilla JS로 변경해 보자
jQuery
<input type="text" class="input1">
<button class="btn2">값 확인</button>
<script>
$('.btn2').click(function(){
const val =$('.input1').val();
alert("입력한 값: " + val);
})
</script>
Vanilla JS - 입력창에 입력한 값 가져오기
<input type="text" class="input1">
<button class="btn2">값 확인</button>
<script>
document.querySelector(".btn2").addEventListener("click",function(){
const val = document.querySelector(".input1").value;
alert("입력한 값: " + val);
})
</script>
- value : 값을 가져오거나 설정할 때 사용
document.getElementById("nameInput").value = "미리 채워진 값";
이런식으로 변경도 가능
728x90
반응형
LIST
'javaScript > Vanilla JS' 카테고리의 다른 글
Vanilla JS 간단한 모달창 열기/닫기 (0) | 2025.04.22 |
---|---|
Vanilla JS 동적 요소 추가/삭제 (0) | 2025.04.21 |
Vanilla JS 반복문, 동적 요소 제어 (0) | 2025.04.20 |
클래스 제어, 토글, 조건문 (0) | 2025.04.18 |
Vanilla JS 기초 연습 - 버튼 클릭하면 alert 띄우기 (0) | 2025.04.17 |
댓글