[JavaScript] onfocus와 onblur

정은아·2022년 10월 3일
0
<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>onfocus와 onblur</title>
  <script>
  function checkFilled(obj)
  {
	  if(obj.value=="")
	  {
		  alert("enter name !!!");
		  obj.focus();		//obj에 다시 포커스

	  }
   }
 </script>
 </head>
 <body onload="document.getElementById('name').focus();">
  <h3>onfocus와 onblur</h3>
<hr>
<p>이름을 입력하지 않고 다른 창으로
이동할 수 없습니다.</p>
<form>
이름 <input type="text" id="name" onblur="checkFilled(this)"></p>
학번 <input type="text">
</form>

 </body>
</html>

onfocus : 포커스가 들어왔을 때
onblur : 포커스를 잃었을 때
onchange : select태그에서 선택을 바꾸었을 때
onkeydown : 키보드를 눌렀을 때

profile
꾸준함의 가치를 믿는 개발자

0개의 댓글