탈

input에 비밀번호 확인 버튼 추가하기

우선, 비밀번호 보기 기능을 적용할 <input>을 <div class="password-container"></div>로 감싸 주세요.

그다음 </div>위 <input> 아래에 <i class="fa fa-lock" id="togglePassword"></i>를 추가해주세요.

 

*(완성된 모습, 기본 예시)

<div class="password-container">

<input type="password" required maxlength="20" placeholder="비밀번호">

<i class="fa fa-lock" id="togglePassword"></i>

</div>

 

 

그다음 아래의 코드를 페이지 원하는 곳에 아래의 코드를 입력해주세요.

 

<!--입력한 비밀번호 볼 수 있도록 구현{-->

<style>

.password-container {

  position: relative;

}

 

#ol_pw {

  width: 100%;

  padding-right: 30px;

}

 

#togglePassword {

  position: absolute;

  right: 10px;

  top: 50%;

  transform: translateY(-50%);

  cursor: pointer;

}

</style>

<script>

// JavaScript

const togglePassword = document.querySelector('#togglePassword');

const password = document.querySelector('#ol_pw');

 

togglePassword.addEventListener('click', function () {

  const type = password.getAttribute('type') === 'password' ? 'text' : 'password';

  password.setAttribute('type', type);

  

  if (type === 'text') {

    togglePassword.classList.remove('fa-lock');

    togglePassword.classList.add('fa-unlock-alt');

  } else {

    togglePassword.classList.remove('fa-unlock-alt');

    togglePassword.classList.add('fa-lock');

  }

});

</script>

<!--}비밀번호 보기 구현 끝-->

 

https://dsclub.kr/code/669

댓글 3개

감사합니다

감사합니다. ^^

좋은 정보 감사합니다.

댓글을 작성하시려면 로그인이 필요합니다.
+
제목 글쓴이 날짜 조회
24-01-27 조회 6,575
24-01-18 조회 7,121
24-01-18 조회 6,579
24-01-17 조회 6,642
24-01-15 조회 7,916
24-01-14 조회 6,848
24-01-12 조회 6,620
24-01-01 조회 7,328
23-12-25 조회 6,640
23-12-24 조회 7,181
23-12-20 조회 6,320
23-12-19 조회 7,130
23-12-18 조회 5,851
23-12-15 조회 6,751
23-12-08 조회 6,531
23-12-05 조회 7,867
23-12-01 조회 6,725
23-11-26 조회 7,059
23-11-21 조회 7,680
23-11-20 조회 7,661
23-11-19 조회 6,687
23-11-16 조회 6,734
23-11-15 조회 6,550
23-11-15 조회 6,385
23-11-12 조회 7,615
23-11-09 조회 7,801
23-11-02 조회 6,677
23-11-02 조회 6,357
23-10-30 조회 6,861
23-10-23 조회 7,632