웹/js

js input 활용

nownow 2022. 6. 26. 20:57
 <div id="login">
    <input type="text">
    <button>Log in</button>
    </div>

html 단에 id로 묶은 입력창과 버튼.

 

const ok1= document.querySelector("#login input");
const ok2 = document.querySelector("#login button")
function whenEvent()
{
    console.log(ok1.value);
}
ok2.addEventListener("click",whenEvent);

해당 id에 있는 input과 button을 각각 할당

버튼에 click 이벤트가 발생했을 때 input에 입력된 값을 콘솔로 보여준다.