[JavaScript] document.getElementById 사용해보기

email 입력란에 입력된 내용 그대로 alert으로 경고창에 출력

 

id는 고유한 식별자이므로 복수로 가져올 수 없음.

getElementsById 가 아니라 getElementById 이므로 s유무에 주의.

 

함수 사용

<script>
    function printEmail(self){
      var useremail = document.getElementById('email').value;
      alert(useremail);
    }
</script>

<body>
    <input type="email" value="Email Address" id="email">
    <input type="submit" value="함수버튼" onclick="printEmail(this)">
</body>

 

바로 출력

<body>
    <input type="email" value="Email Address" id="email">
    <input type="submit" value="바로출력버튼" onclick="alert(document.getElementById('email').value)">
</body>