0

I don't know what I am doing wrong here. Scratching my head wondering why my function isn't working. Pretty much trying to toggle "Show/Hide Password".

Currently, I am able to switch from password to text, but not able to switch back to password. Anything you see wrong?

HTML:

<div class="form-group mbs">
  <label for="password" class="label-medium mbxs">Password</label><br>
  <input type="password" name="password" class="form-control input-medium" required>
</div>

<div class="form-group mbs">
  <label for="passwordConfirm" class="label-medium mbxs">Password Confirm</label><br>
  <input type="password" name="passwordConfirm" class="form-control input-medium mbs" required><br>
  <a href="" id="generate-password" class="btn btn-large btn-green-gradient">Generate Password</a>
  <a href="" id="show-password" class="btn btn-large btn-red-gradient">Show/Hide Password</a>
</div>

Scripts:

$("#show-password").click(function(e){
e.preventDefault();
if($("input[name='password']").attr('type', 'password') && $("input[name='passwordConfirm']").attr('type', 'password')){
  $("input[name='password']").attr('type', 'text');
  $("input[name='passwordConfirm']").attr('type', 'text');
}else{
  $("input[name='password']").attr('type', 'password');
  $("input[name='passwordConfirm']").attr('type', 'password');
}

});

Taylor Foster
  • 1,052
  • 9
  • 19

3 Answers3

3

You are assigning the attribute at if condition. Change it like this,

$("#show-password").click(function(e) {
  e.preventDefault();
  if ($("input[name='password']").attr('type') == 'password' && $("input[name='passwordConfirm']").attr('type') == 'password') {

    $("input[name='password']").attr('type', 'text');
    $("input[name='passwordConfirm']").attr('type', 'text');
  } else {

    $("input[name='password']").attr('type', 'password');
    $("input[name='passwordConfirm']").attr('type', 'password');
  }
});

Fiddle

if($("input[name='password']").attr('type', 'password') && $("input[name='passwordConfirm']").attr('type', 'password')) will always return true.

Anoop Joshi
  • 24,460
  • 8
  • 28
  • 49
1

The way you are using .attr will set the attribute instead of comparing it's type

$("#show-password").click(function(e){
    e.preventDefault();
    if($("input[name='password']").attr('type')=='password' && // Changed here
    $("input[name='passwordConfirm']").attr('type')== 'password')
    {
      $("input[name='password']").attr('type', 'text');
      $("input[name='passwordConfirm']").attr('type', 'text');
    }else{

  $("input[name='password']").attr('type', 'password');
  $("input[name='passwordConfirm']").attr('type', 'password');
}
});

Working jsfiddle

EDIT As pointed out by Anoop Joshi there is no difference between his & my answer. So modified a little , by using a common class between the two input type = "password" & used ternary operator to evaluate it

HTML

<div class="form-group mbs">
  <label for="password" class="label-medium mbxs">Password</label><br>
   // Used a common class passwordToggle 
  <input type="password" name="password" class="passwordToggle form-control input-medium" required>
</div>

<div class="form-group mbs">
  <label for="passwordConfirm" class="label-medium mbxs ">Password Confirm</label><br>
  // Used a common class passwordToggle 
  <input type="password" name="passwordConfirm" class="form-control input-medium mbs passwordToggle" required><br>
  <a href="" id="generate-password" class="btn btn-large btn-green-gradient">Generate Password</a>
  <a href="" id="show-password" class="btn btn-large btn-red-gradient">Show/Hide Password</a>
</div>

JS

$("#show-password").click(function(e){
e.preventDefault();
// Minimizing lines of code using ternary operator
$(".passwordToggle").attr('type') == 'password'? $(".passwordToggle").attr('type','text'):$(".passwordToggle").attr('type','password')
});

jsfiddle using ternary operator

Community
  • 1
  • 1
brk
  • 43,022
  • 4
  • 37
  • 61
1

you need to use .attr('type')=='password'. instead of .attr('type', 'password') in condition.Try below code

$("#show-password").click(function(e){ e.preventDefault(); if($("input[name='password']").attr('type')=='password' && $("input[name='passwordConfirm']").attr('type')=='password'){ $("input[name='password']").attr('type', 'text'); $("input[name='passwordConfirm']").attr('type', 'text'); }else{ $("input[name='password']").attr('type', 'password'); $("input[name='passwordConfirm']").attr('type', 'password'); } });