jQuery - val() 메소드

Yuri Lee·2021년 2월 5일
0

배경

그냥 항상 무의식적으로 써오던 메소드에 대해 알아보려고 한다.

.val()

양식(form)의 값을 가져오거나 값을 설정하는 메소드이다.

var test = $('input#yuri').val();

위 코드는 id가인 input 요소의 값을 변수 test에 저장한다는 뜻이다.

예시

<!doctype html>
<html lang="ko">

<head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $('button#inputBtn').click(function () {
                var test = $('input#yuri').val();
                alert(test);
            });
        });
    </script>
</head>

<body>
    <p>
        <input type="text" id="yuri">
        <button id="inputBtn">Click</button>
    </p>
</body>

</html>

다음의 코드를 실행해보자! 😎 😎

input 값에 yuri를 입력했더니 alert 창으로 입력한 값이 그대로 나온 것을 볼 수 있다.


https://www.codingfactory.net/10765

profile
Step by step goes a long way ✨

0개의 댓글