Place a button after an input and show it's password in plain text I have managed to achieve this action through this:



function togglePasswordVisibility($pw, on, id) {
  $pw.attr('type', on ? 'password' : 'text');
  $('[data-id=' + id + '] &gt; i').toggleClass('fa-eye-slash fa-eye');
 }

 // $("#pass-on").after('&lt;div class="input-group-append"&gt;&lt;span class="password-button password-button-main"&gt;&lt;i class="fas fa-eye-slash"&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;');

 $('[data-id]').on('click', function() {
  var id = $(this).data('id'),
   $pw = $('#' + id);

  togglePasswordVisibility($pw, false, id);

  setTimeout(function() {
   togglePasswordVisibility($pw, true, id);
  }, 800);
 });
&lt;link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

&lt;div class="input-group"&gt;
       &lt;input type="password" id="pass-on" title="&lt;?php echo $user-&gt;valid_password_error; ?&gt;" class="form-control" name="txt_upass" placeholder="Enter Password" autocomplete="off" value="" oninvalid="setCustomValidity('&lt;?php echo $user-&gt;password_error; ?&gt;')" oninput="setCustomValidity('')" required /&gt;
     &lt;label&gt;Password&lt;/label&gt;
     &lt;div class="input-group-append"&gt;
         &lt;span class="password-button password-button-main" data-id="pass-on"&gt;&lt;i class="fas fa-eye-slash"&gt;&lt;/i&gt;&lt;/span&gt;
       &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group progress jquery-result-1" style="height: 10px;"&gt;


     &lt;div class="form-group input-group"&gt;
      &lt;input type="password" id="pass-verify-on" title="&lt;?php echo $user-&gt;valid_password_error; ?&gt;" class="form-control" name="txt_upass_ok" placeholder="Retype Password" autocomplete="off" value="" oninvalid="setCustomValidity('&lt;?php echo $user-&gt;password_error; ?&gt;')" oninput="setCustomValidity('')" required /&gt;
      &lt;label&gt;Retype Password&lt;/label&gt;
      &lt;div class="input-group-append"&gt;
          &lt;span class="password-button password-button-verify" data-id="pass-verify-on"&gt;&lt;i class="fas fa-eye-slash"&gt;&lt;/i&gt;&lt;/span&gt;
        &lt;/div&gt;
     &lt;/div&gt;




But I would like to place the password button from within the function using the commented line with the after callback and making it apply the same effect. Right now the button correlates with the input through the association of data-id and id. If I would to place it inside the function I would have to get rid of the data-id and still make it act individually on each input, I just can't figure how. Thank you for your time.
You can first create the i element and add a click event handler to it dynamically while you wrap it inside the div and span and add it to the document.

As you use jQuery, I would suggest using it for creating each individual element, providing it with attributes using jQuery methods. 

Here is how you could do it:



function togglePasswordVisibility($pw, on, $eye) {
    $pw.attr('type', on ? 'password' : 'text');
    $eye.toggleClass('fa-eye-slash fa-eye');
}

$("[type=password]").each(function () {
    var $pw = $(this);
    var $eye = $("&lt;i&gt;").addClass("fas fa-eye-slash").click(function () {
        togglePasswordVisibility($pw, false, $eye);
        setTimeout(function() {
            togglePasswordVisibility($pw, true, $eye);
        }, 800);
    });
    $pw.parent().append(
        $("&lt;div&gt;").addClass("input-group-append").append(
            $("&lt;span&gt;").addClass("password-button password-button-main").append($eye)
        )
    );
});
&lt;link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

&lt;link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/&gt;

&lt;div class="input-group"&gt;
    &lt;input type="password" id="pass-on" title="&lt;?php echo $user-&gt;valid_password_error; ?&gt;" class="form-control" name="txt_upass" placeholder="Enter Password" autocomplete="off" value="" oninvalid="setCustomValidity('&lt;?php echo $user-&gt;password_error; ?&gt;')" oninput="setCustomValidity('')" required /&gt;
    &lt;label for="pass-on"&gt;Password&lt;/label&gt;
&lt;/div&gt;
                
&lt;div class="form-group input-group"&gt;
    &lt;input type="password" id="pass-verify-on" title="&lt;?php echo $user-&gt;valid_password_error; ?&gt;" class="form-control" name="txt_upass_ok" placeholder="Retype Password" autocomplete="off" value="" oninvalid="setCustomValidity('&lt;?php echo $user-&gt;password_error; ?&gt;')" oninput="setCustomValidity('')" required /&gt;
    &lt;label for="pass-verify-on"&gt;Retype Password&lt;/label&gt;
&lt;/div&gt;