I am trying to create a show/hide password feature, but unfortunately, it's not functioning

Can anyone help me figure out why my show/hide password functionality isn't working properly? I've tried to toggle the visibility of the password using an eye icon, but it's not hiding the password correctly.

const Password = document.getElementsByClassName("teks");
const eye = document.getElementById("eye");
eye.addEventListener("click", function () {
  if (Password.type === "password") {
    Password.type = "text";
    eye.classList.add("hide");
  } else {
    Password.type = "password";
    eye.classList.remove("hide");
  }
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<div id="login-form">
  <h3>Login</h3>
  <ul class="login-list">
    <i class="fa fa-times" aria-hidden="true" id="cancel"></i>
    <input class="teks1" type="text" placeholder="Username" />
    <input class="teks" type="password" placeholder="Password" />
    <span class="mata">
      <i class="fa fa-eye" id="eye"></i>
    </span>
    <p><a href="">Lupa password?</a><br /></p>
    <input type="button" value="Login" class="tombol" />
  </ul>
</div>

I am looking to create an eye icon that toggles between displaying and hiding the password for a user-friendly login experience. Let's debug this together!

Answer №1

const Password = document.getElementsByClassName("teks")

outputs array. To make it accessible, you should specify the index or assign an id and use getElementById.

const Password = document.getElementsByClassName("teks")[0];

const Password = document.getElementsByClassName("teks")[0];
const eye = document.getElementById("eye");
eye.addEventListener("click", function () {
  if (Password.type === "password") {
    Password.type = "text";
    eye.classList.add("hide");
  } else {
    Password.type = "password";
    eye.classList.remove("hide");
  }
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<div id="login-form">
  <h3>Login</h3>
  <ul class="login-list">
    <i class="fa fa-times" aria-hidden="true" id="cancel"></i>
    <input class="teks1" type="text" placeholder="Username" />
    <input class="teks" type="password" placeholder="Password" />
    <span class="mata">
      <i class="fa fa-eye" id="eye"></i>
    </span>
    <p><a href="">Lupa password?</a><br /></p>
    <input type="button" value="Login" class="tombol" />
  </ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to acquire a reference to a dynamically generated child element with an unknown name in JSON

JSON data is structured as follows: "json": { "schema": { "type": "object", "title": "My Table", "properties": { "table-1659555454697": { ...

Modify a JavaScript object in JSON format using another object as reference

Consider two JSON formatted JavaScript objects: obj1 = { prop1: 1, prop2: 2, prop3: 3 } obj2 = { prop1: 1, prop2: 3 } In the context of jQuery or Angular, what is the recommended practice to update obj2 into obj1 while also re ...

What is the best way to test an AngularJS directive and monitor function calls with a spy?

After running the code below, an error is thrown mentioning that element.popover is not being invoked. I can't seem to identify what the problem is. Thank you in advance for any assistance provided. directive: angular.module('directives', ...

Why does it fire off numerous requests even though I only called it once?

Everything seemed to be working fine with my project. However, I noticed in the console network that one of my GET requests is being sent twice even though I only triggered it once. View network console here If I comment out the entire created function co ...

Manipulate Codeigniter Dropdown to Display Date/Time Query Results

I am relatively new to the world of codeigniter and MVC architecture. Currently, I am in the process of transforming an outdated coldfusion MX website into a fresh new design and layout. My current task involves populating a dropdown menu from a database ...

Run the script within the Angular scope

Hey there! I'm having an issue passing a JavaScript code through Angular scope, but when it renders on the view page, it's displayed as text. I even attempted using ng-Sanitize, but unfortunately, that didn't solve the problem. &lt;div ...

Use two queries to apply filters to entries in NextJS

Greetings to all! I am currently working on a project in NextJS that involves showcasing a portfolio of works generated from JSON data. [ { "title": "WordPress Plugin for Yandex Recommender Widget", "image" ...

Capybara's attach_file function is not properly activating the React onChange handler in Firefox

Currently, I am conducting tests on the file upload feature of a React-built page. The page includes a hidden file input field with an onChange event listener attached to it. Upon selecting a file, the onChange event is triggered and the file is processed ...

A guide on toggling classes in Ember

I've been exploring ways to toggle a class within a component. The component I'm working with has two states: active and inactive. To activate it, I need to add the class active. Currently, I'm using jQuery's addClass and removeClass ...

Endless Keycloak redirection loop

We have integrated Keycloak 2.3.0.Final into our system and are utilizing the Javascript adapter located at /auth/js/keycloak.js. Our application, built using React+Redux, encounters an issue during the authentication process outlined in the documentation. ...

Creating an HTML Excel file in ASP.NET

I am using a StringBuilder to dynamically generate a file. The file contains a table with two rows and two columns. In the second row, the first column has text and the second column contains another table. I have successfully created this design in HTML ...

Combining Angular 2.0 within Angular 1.x: Elevating your module

Currently, I am attempting to incorporate an Angular 2.0 component within an Angular 1.x application (for experimentation and learning purposes). Upon further examination, I have observed that this can be achieved by referencing the Angular2 upgrade modul ...

Discover the magic of unlocking XML content with the power of XSL!

Hello, I am currently working on transforming an XML archive using an XSL archive. My goal is to generate a table displaying the content of the XML, but I am encountering difficulties in showing the values of two columns named "complexity" and "subject". B ...

What are the steps to create a table using divs and CSS?

Can someone guide me on how to create a table using divs, CSS, and proper XHTML formatting? <table width="100%" border="1"> <tr> <td style="width: 130px">1</td> <td align="center">2</t ...

Accessing elements of a multi-dimensional Javascript array: A step-by-step guide

In my Java script array, the structure goes up to 5 levels deep with elements containing strings and arrays. photoList = [Photographs, [ [2000, [ [London, [pic1.jpg, pic2.jpg, pic3.jpg]], ...

Improving JavaScript performance for smooth rendering across different browsers at a high frame rate

I have a fast Fourier transform (FFT) visualization using canvas that displays data at a high speed. My goal is to enhance the code so that I can have 16 browser windows open simultaneously, each running at 60 frames per second or close to it. Currently, m ...

The integration of ASPNetSpell Spell Checker with CKEditor 3 is not functioning properly on Chrome browsers

In my efforts to incorporate ASPNetSpell's spell checker () into CKEditor (), I have encountered some unexpected behavior across different browsers - IE 8+, FireFox 12, and Chrome 18.0.1025.152m. The issue seems to be that in Chrome, ASPNetSpell is u ...

"Utilize Bootstrap's responsive 3-column grid layout for a sleek design, complete with hidden col-12 elements below each grid

Looking to create a 100% width column below an element within a column of a bootstrap grid. For better understanding, here is what I'm aiming for: https://i.stack.imgur.com/pl1Fi.png On selecting one of the images (from 1 to x), a hidden div with di ...

Issue encountered in IE9 and IE10 when loading the angular library results in the "SCRIPT5007: Object expected" error for Angular JS

I am currently working on an AngularJS application that needs to be compatible with Firefox, IE 9, and IE 10. We are using the latest version of the AngularJS library (currently at 1.3.15). Our serverside is based on Java in the JavaEE platform, running on ...

Adding a NVD3 chart into a Leaflet popup

My AngularJs web application incorporates a map built with Leaflet.js and Leaflet markercluster. For rendering charts, I utilize nvd3.js along with the nvd3-angular-directive. The data I have represents various countries worldwide, each displayed on the m ...