0

I have a function that is in Javascript that runs once (by calling id). I tried converting the function into jQuery to allow for the function to work for each instance of pwd.

How do I get the jQuery code to work like the JS code, but run for every instance of pwd?

$(".pass").on("click", ".typcn-eye", function() {
  var pwd = $(this).closest(".pwd");

  function togglePass() {
    $(this).toggleClass("active");

    pwd.type == "password" ? (pwd.type = "text") : (pwd.type = "password");
  }
});

var pwd = document.getElementById('pwd');
var eye = document.getElementById('eye');

eye.addEventListener('click', togglePass);

function togglePass() {

  eye.classList.toggle('active');

  (pwd.type == 'password') ? pwd.type = 'text': pwd.type = 'password';
}
.box {
  display: flex;
  flex-direction: column;
  align-items: start;
}

.pass {
  display: flex;
  align-items: center;
  border: 1px solid;
}

.box input[type="text"],
.box input[type="password"] {
  display: block;
  margin: 20px auto;
  background: #ececec;
  border: 0;
  border-radius: 5px;
  padding: 14px 10px;
  width: 220px;
  outline: none;
  color: #565f79;
}

.typcn {
  color: #3b476b;
  font-size: 22px;
  cursor: pointer;
}

.typcn.active {
  color: #7f60eb;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/typicons/2.0.9/typicons.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="box">
  <div class="pass">
    <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd" autocomplete="off">
    <i class="typcn typcn-eye" id="eye"></i>
  </div>
  <div class="pass">
    <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd2" autocomplete="off">
    <i class="typcn typcn-eye" id="eye2"></i>
  </div>
</form>

2
  • 1
    Here you go: jsfiddle.net/khrismuc/guwLn5tb Commented Jan 15, 2019 at 19:34
  • Working. Thanks Commented Jan 15, 2019 at 19:39

1 Answer 1

1

On the jQuery version in your click event you have the function togglePass() but you never trigger it. Since you do in your other version.

// For triggering you function
$(".pass").on("click", ".typcn-eye", togglePass);

// Function outside your click event
function togglePass() {
    // Toggle active class on every click
    $(this).toggleClass("active");
    // Get the type of the prev element 
    let type = $(this).prev().attr("type"),
        changeTo = type === "password" ? "text" : "password";

    // Change type attr of that element
    $(this).prev().attr("type", changeTo);
}
.box {
    display: flex;
    flex-direction: column;
    align-items: start;
}

.pass {
    display: flex;
    align-items: center;
    border: 1px solid;
}

.box input[type="text"],
.box input[type="password"] {
    display: block;
    margin: 20px auto;
    background: #ececec;
    border: 0;
    border-radius: 5px;
    padding: 14px 10px;
    width: 220px;
    outline: none;
    color: #565f79;
}

.typcn {
    color: #3b476b;
    font-size: 22px;
    cursor: pointer;
}

.typcn.active {
    color: #7f60eb;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/typicons/2.0.9/typicons.min.css" rel="stylesheet" />
<form class="box">
    <div class="pass">
        <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd" autocomplete="off">
        <i class="typcn typcn-eye" id="eye"></i>
    </div>
    <div class="pass">
        <input class="pwd" type="password" name="password" placeholder="Passsword" id="pwd2" autocomplete="off">
        <i class="typcn typcn-eye" id="eye2"></i>
    </div>
</form>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.