Tips for preventing the password field from displaying 'null' as a placeholder

Iā€™m currently facing an issue with my password field that has some behind-the-scenes JavaScript functionality. Whenever I click on the field, it displays 'null' as a value, as shown in the image.

https://i.sstatic.net/qvZLI.png

This only happens when clicking on the field, and there is no placeholder set for the input. How can I prevent this from occurring?

Below is the code snippet:

//floating input controller

const FloatLabel = (() => {

  // add active class
  const handleFocus = (e) => {
    const target = e.target;
    target.parentNode.classList.add('active');
    target.setAttribute('placeholder', target.getAttribute('data-placeholder'));
  };

  // remove active class
  const handleBlur = (e) => {
    const target = e.target;
    if (!target.value) {
      target.parentNode.classList.remove('active');
    }
    target.removeAttribute('placeholder');
  };

  // register events
  const bindEvents = (element) => {
    const floatField = element.querySelector('input');
    floatField.addEventListener('focus', handleFocus);
    floatField.addEventListener('blur', handleBlur);
  };

  // get DOM elements
  const init = () => {
    const floatContainers = document.querySelectorAll('.float-container');

    floatContainers.forEach((element) => {

      if (element.querySelector('input').value) {
        element.classList.add('active');
      }

      bindEvents(element);
    });
  };

  return {
    init: init
  };
})();

FloatLabel.init();
.float-container {
  position: relative;
  background-color: #dfecff;
  width: 100%;
  border-radius: 5px;
  transition: 150ms;
  padding-top: .6em;
  padding-bottom: .6em;
  border: 2px solid #b9d6ff;
}

.float-container:hover {
  background-color: #1a73e8;
  cursor: text;
}

.password-toggle-button {
  position: absolute;
  font-size: 1em;
  padding-top: 8px;
  color: #333333;
  margin-left: 93%;
}

.password-toggle-button:hover {
  cursor: pointer;
  color: #eeeeee;
}

label {
  font-size: 1em;
  font-family: Arial, Helvetica, sans-serif;
  color: #5e5e5e;
  position: absolute;
  margin-left: 3%;
  z-index: 140;
  transform: translate(0, 0) scale(1);
  pointer-events: none;
  user-select: none;
  transform-origin: top left;
  transition: all .1s ease-in-out;
}

.float-container input {
  color: #eeeeee;
}

.float-container.active label {
  transform: translate(-0.5em, -1.5em) scale(.95);
  font-style: italic;
  font-weight: 200;
  border-radius: 10px;
  background-color: #1a73e8;
  padding-left: .5em;
  padding-right: .5em;
  color: #eeeeee;
}

.float-container.active input {
  color: #eeeeee;
}

.float-container.active {
  background-color: #1a73e8;
  border: 2px solid #eeeeee;
}

.form-center {
  display: relative;
  margin-top: 3em;
  margin-left: 5vw;
  margin-bottom: 5em;
  word-break: break-all;
  padding-top: 3%;
  padding-left: 5vw;
  padding-right: 3vw;
  padding-bottom: 5%;
  width: 60vw;
  line-height: 2em;
  background-color: #1a73e8;
  border-radius: 20px;
  border-width: 3px;
  border-style: solid;
  border-color: #333333;
}

input{
  border: none;
  background-color: transparent;
  color: #333333;
  outline: 0;
  margin-top: auto;
  margin-left: 3%;
  margin-right: 20%;
  width: 90%;
  z-index: 150;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css" rel="stylesheet" />

<div class="form-center"><div id="float-container" class="float-container" onclick="document.getElementById('pw').focus(); return false;">
  <label for="pw" class="inner-label">Password</label>
  <i class="fa fa-eye password-toggle-button" aria-hidden="true"></i>
  <input type="password" class="floatField id="pw" placeholder="">
</div>
</div>

Answer ā„–1

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css" rel="stylesheet" />

<div class="form-center"><div id="float-container" class="float-container" onclick="document.getElementById('pw').focus(); return false;">
  <label for="pw" class="inner-label">Password</label>
  <i class="fa fa-eye password-toggle-button" aria-hidden="true"></i>
  <input type="password" class="floatField id="pw">
</div>
</div>

What happens if you decide to eliminate the placeholder text?

Answer ā„–2

Before assigning a value as the input placeholder, it is important to confirm that the value is not null:

const handleFocus = (e) => {
    const target = e.target;
    target.parentNode.classList.add('active');
    const placeholder = target.getAttribute('data-placeholder');

    if (!!placeholder && placeholder !== 'null') {
        target.setAttribute('placeholder', placeholder);
    } else {
        // Implement an alternative action
    }
};

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

Mocha fails to detect the Constructor Function

I am at a loss for ideas on what could be causing the issue here, so I'm hoping someone can provide some assistance. Below is the script I am testing; function MyFunc() { this.test = 'hello world'; }; module.exports = MyFunc; Here ...

What causes my horizontal scroll bar to disappear while the vertical scroll bar remains visible?

Here are the CSS entries I am using... div { overflow: hidden; overflow-y: hidden; overflow-x: hidden;} div:hover { overflow:visible; overflow-y: visible; overflow-x: visible;} I expected the vertical and horizontal scroll bars to only appear when a user ...

CSS for Aligning Navigation Bar Min-Width

Can someone assist me with properly scaling my navbar (e.g. min-width 800px) and setting up the phone number div with space between it and the links? This is my first try at this, so any help is greatly appreciated. Thank you. CSS <style type="text/cs ...

Automatically open Bootstrap dropdown upon loading the page

Displayed below is my dropdown menu styled using bootstrap: <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="posuvnik">15min <strong class="caret"></strong></a> <ul class="dropd ...

The generation of React Native Jest coverage results may vary depending on how an ES6 module is exported

I am working with a React Native button component that consists of its own index.js and styles.js files. The content of styles.js is as follows: import { StyleSheet } from "react-native"; export default StyleSheet.create({ container: { borderRadius ...

Scheduled Job unable to complete post request

(I am completely new to the world of JavaScript, node.js, and Heroku so I apologize in advance if my question is not very clear) I recently set up a Heroku node.js application with a scheduled task that should run every hour. The task does run as expecte ...

Navigating through the directories in PUG using the absolute path

Referring to the docs for PUG (), it states: If a path is absolute (example: include /root.pug), it gets resolved by prepending options.basedir. Otherwise, paths are resolved in relation to the file being compiled. To clarify, I understand that this in ...

What is the most effective way to compare a nested array using the map or filter function in order to return only the first match

Here is a code snippet showcasing the data object containing information for the codeworks array with code and text values. There is a key array named code = ["ABC","MDH"] and the expected output is displayed in the following code snippets. const data = ...

Mismatched units px and rem (existing fixes are ineffective)

Recently, I attempted to implement custom sass in a project at work. However, when running npm watch, I encountered the error: Incompatible units px and rem. The root of the issue seems to be in _variables.scss where the following line resides: $input-h ...

Tslint is notifying that JSX elements without any children must be self-closing to prevent errors [Error]

Recently, I encountered an issue while trying to build my solution using the command npm run build. The error message displayed was: JSX elements with no children must be self-closing. I came across a similar problem on Stack Overflow but unfortunately ...

Is it possible to drag the parent element but only select the text within the child

How can I make the parent div draggable, yet keep the text in the child span selectable? <aside draggable="true" id="dragme"> This is an aside, drag me. <span id="copyme" draggable="false">But copy me!</span> </aside> A simila ...

Looking for assistance with fundamental HTML concepts

I'm struggling to make the navigation bar stretch to full height. As a beginner in HTML and CSS, I have limited knowledge about it. I have tried numerous solutions found on the internet but nothing seems to work. The styling and HTML code are provide ...

Using React to implement a two-way binding for text input, where the format differs between the stored value and the

I am seeking a solution for synchronizing a list of text inputs with values stored in the backend. Essentially, I want changes made to the text inputs to update the corresponding values and vice versa. This scenario calls for a typical 2-way binding funct ...

The Divs pile one on top of the other

I am trying to position the second div below the first div instead of stacking on top of it and making the first div invisible. How can I achieve this? .shop { width: 100%; height: 1000px; background-color: #ffffff; position: relative; ...

I am attempting to rebuild Vuex's Getting Started example by utilizing multiple components, yet I am struggling to understand how to call root methods from within these components

The project I'm working on can be found here. It is a simple one, but for the purpose of learning, I divided it into index and four js files (parent, child, root, and store). My challenge lies in figuring out how to call the increment and decrement ro ...

Pick just one choice within Bootstrap's radio buttons

Here is the code snippet I am using for my radio selection: <div id="box" class="col-md-4"> <div class="panel panel-default"> <div class="panel-heading" id="ph"> <h3 ...

Original: Placeholder Text With $scope.Array and HTML DIVRewritten: Text

How can I display default text as a placeholder in a drop-down menu without including it as an option? HTML <div class="form-group"> Upload new file to: <select class="form-control" ng-model="selectedDocumentType" ng-click="s ...

Techniques for transferring child properties and values to parent components upon the screen being initialized

Is there a way to pass property values from a child component to a parent component when the react app is first loaded? In my scenario, I have a parent component named app.js that renders a home component containing a JSON component. Initially, upon loadi ...

What is the best way to manage uploading images with varying proportions in React Js?

I've been scouring the internet for the best way to upload images of varying sizes (width and height) while still being able to display them neatly in a gallery format. An ideal example is the Facebook gallery, where all the pictures are perfectly al ...

When fetching data from the API in Angular, the response is displayed as an object

After fetching data from the API, I am encountering an issue where the "jobTitle" value is not displaying in the table, but instead appears as an array in the console. Can someone assist me with resolving this problem? Below is the visibility.ts file: exp ...