탈

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 조회 5,456
24-01-18 조회 5,985
24-01-18 조회 5,346
24-01-17 조회 5,121
24-01-15 조회 6,254
24-01-14 조회 5,685
24-01-12 조회 5,419
24-01-01 조회 6,097
23-12-25 조회 5,504
23-12-24 조회 6,054
23-12-20 조회 5,189
23-12-19 조회 6,020
23-12-18 조회 4,705
23-12-15 조회 5,608
23-12-08 조회 5,392
23-12-05 조회 6,698
23-12-01 조회 5,595
23-11-26 조회 5,965
23-11-21 조회 6,548
23-11-20 조회 6,526
23-11-19 조회 5,559
23-11-16 조회 5,612
23-11-15 조회 5,390
23-11-15 조회 5,245
23-11-12 조회 6,459
23-11-09 조회 6,648
23-11-02 조회 5,546
23-11-02 조회 5,193
23-10-30 조회 5,740
23-10-23 조회 6,502