Having trouble displaying the input upon clicking the icon

Currently, I am honing my skills in vanilla JavaScript by working on a website project. In this project, I have implemented a feature where clicking on a fingerprint icon triggers an input field to appear for the user to enter their password. The code snippet below showcases what I have attempted, with some inspiration from .

However, despite my efforts, I seem to be facing issues getting it to function correctly. I would greatly appreciate any assistance in troubleshooting and fixing the code.

Code

var show = function(elem) {
  elem.style.display = 'block';
};

var hide = function(elem) {
  elem.style.display = 'none';
};
var toggle = function(elem) {
  elem.classList.toggle('wash');
};

// Listen for click events
document.addEventListener('click', function(event) {

  // Ensure that the clicked element is our toggle
  if (!event.target.classList.contains('wash')) return;

  // Prevent default link behavior
  event.preventDefault();

  // Obtain the content
  var content = document.querySelector(event.target.hash);
  if (!content) return;

  // Toggle the content
  toggle(content);

}, false);
h1 {
  font-family: 'Libre Baskerville', serif;
  text-align: center;
  font-size: 4em;
  color: #e0e4ec
}

html {
  background: #556c9a
}

.keys {
  text-align: center;
}

.fa-fingerprint {
  text-align: center;
  padding-left: 2%;
  padding-right: 2%;
  font-size: 3rem;
}

h2 {
  position: relative;
  text-align: center;
}

.press:hover {
  color: yellow;
}

.type {
  display: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Hello</title>
  <link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:ital@1&display=swap" rel="stylesheet">
  <script src="https://kit.fontawesome.com/923b4d30ed.js" crossorigin="anonymous"></script>
  <h1>Pineapple</h1>
</head>

<body>


  <div class="keys">
    <span class="press"><i class="fas fa-fingerprint"></i><h2>Log In</h2></span>
    <span class="type"><input placeholder="Password"></span>
  </div>
</body>

</html>

Answer №1

To achieve this in vanilla JavaScript, you would need to target an element by its ID and toggle its display state.

function myFunction() {
  var x = document.getElementById("my");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
 h1{
  font-family: 'Libre Baskerville', serif;
  text-align: center;
  font-size: 4em;
  color:#e0e4ec
}
html{
  background: #556c9a
}
.keys{
  text-align:center;

}
.fa-fingerprint{
  text-align:center;
  padding-left:2%;
  padding-right:2%;
  font-size:3rem;
}
h2{
  position:relative;
  text-align:center;
}
.press:hover{
  color:yellow;
}
.type{
  display:none;
}
   <!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">
  <title>Hello</title>
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:ital@1&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/923b4d30ed.js" crossorigin="anonymous"></script>
  <h1>Pineapple</h1>
  </head>
  <body>


    <div class="keys">
      <span class="press"  onclick="myFunction()"><i class="fas fa-fingerprint"></i><h2>Log In</h2></span>
      <span class="type" id="my"><input  placeholder="Password"></span>
    </div>
  </body>

</html>

Answer №2

To reveal the input box, it is necessary to include an onclick() method in your <span> element that triggers a change in the display property from none to block.

You can find the solution by clicking on this link.

Answer №3

To achieve this in Javascript, you can simply check the property of block and none.

  1. The input type is a self-closing element, so you do not need to add </input> at the end.
  2. You should set the display property of the input type and based on the click event, you can toggle according to the condition.

document.addEventListener('click', function(event) {
  var type = document.getElementById('inputType'); // the type you want to hide/show
  if (type.style.display !== 'none') {
    type.style.display = 'none';
  } else {
    type.style.display = 'block';
  }
});
h1 {
  font-family: 'Libre Baskerville', serif;
  text-align: center;
  font-size: 4em;
  color: #e0e4ec
}

html {
  background: #556c9a
}

.keys {
  text-align: center;
}

.fa-fingerprint {
  text-align: center;
  padding-left: 2%;
  padding-right: 2%;
  font-size: 3rem;
}

h2 {
  position: relative;
  text-align: center;
}

.press:hover {
  color: yellow;
}
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:ital@1&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/923b4d30ed.js" crossorigin="anonymous"></script>
<h1>Pineapple</h1>

<body>
  <div class="keys">
    <span class="press"><i class="fas fa-fingerprint"></i><h2>Log In</h2></span>
    <span id="inputType" style="display:none;"><input placeholder="Password"/></span>
  </div>
</body>

Answer №4

To toggle between displaying the <input> as either none or block, you can simply add an event listener to the .press class.

document.getElementsByClassName('press')[0].addEventListener('click', () => {
  var input = document.getElementsByClassName('type')[0];
  input.style.display = (input.style.display == 'none') ? 'block' : 'none';
});
h1 {
  font-family: 'Libre Baskerville', serif;
  text-align: center;
  font-size: 4em;
  color: #e0e4ec
}

html {
  background: #556c9a
}

.keys {
  text-align: center;
}

.fa-fingerprint {
  text-align: center;
  padding-left: 2%;
  padding-right: 2%;
  font-size: 3rem;
}

h2 {
  position: relative;
  text-align: center;
}

.press:hover {
  color: yellow;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <title>Hello</title>
  <link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:ital@1&display=swap" rel="stylesheet>
  <script src="https://kit.fontawesome.com/923b4d30ed.js" crossorigin="anonymous></script>
  <h1>Pineapple</h1>
</head>

<body>


  <div class="keys>
    <span class="press"><i class="fas fa-fingerprint"></i><h2>Log In</h2></span>
    <span class="type" style="display: none;"><input placeholder="Password"></span>
  </div>
</body>

</html>

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

How to retrieve a refined selection of items

In my scenario, there are two important objects - dropdownOptions and totalItem https://i.sstatic.net/VreXd.png The requirement is as follows: If 12 < totalItems < 24, then display "Show 12, Show 24" If 24 < totalItems < 36, only show "Show ...

Utilizing AngularJS: A guide to implementing and manipulating arrays within HTML values

I've developed a unique custom directive incorporating the fields: ng-model, input-array <mydir ng-model="myBinding" input-array="input1,input2"></mydir> Data bindings for input 1 and input 2 are set up, but how can I intelligently gener ...

JavaScript pause until image finishes uploading

The code snippet below contains 3 alert() functions that should be fired in order: first alert(1), then alert(2), and finally alert(3). However, due to the img.onload() function, alert(3) is being triggered before alert(2). var _URL = window.URL || window ...

Limit how API call costs are set in a function by throttling based on an argument

I am currently implementing express-throttle to restrict the number of API calls per IP address each day. I would like to dynamically set the 'cost' parameter in the 'options' array based on a value from the API request (refer to commen ...

"Although disabled, input elements can still be focused on in Firefox browser

Illustrative example let userInput = document.createElement("input"); userInput.id = "user-input"; userInput.type = "number"; userInput.className = "user-number-input"; userInput.disabled = true; document.body.appendChild(userInput); .number-inp ...

Utilize the JSSOR Slider to dynamically pull images from an online album, such as by integrating with an RSS Feed

My main motivation for exploring this possibility is the negative impact that loading images into the slider has on my website's performance. Is there a method to import images from an externally hosted album, such as Google Picasa albums (or any othe ...

If I dared to eliminate the emphasized line, this code would completely fall apart

<!DOCTYPE html> <html> <head> </head> <body> <h1 id="message-el">Ready to play?</h1> <p id="cards-el"></p> <p id="sum-el"></p> <butto ...

Measuring dimensions with HTML on the server - how to do it?

I am facing a challenge with my web application that allows users to upload entire .html files to the server. I am looking for a way to automatically detect and store the width/height of the uploaded html in my database. Currently, I have been experimenti ...

Design a visually stunning image grid with the use of Bootstrap 4

I am having difficulty creating an image grid with consistent spacing between columns, like shown in the image below: https://i.sstatic.net/azsxa.jpg The issue I am facing is aligning the right margin (red line), as illustrated below: https://i.sstatic. ...

Is it possible to utilize $.each() in combination with $.ajax() to query an API?

I am dealing with an array containing 2 values, and for each value, I need to make an AJAX query to an API to check the stock availability. If there is stock for both values, a certain message should be executed, otherwise, a different message. This check ...

Using the Vuex getter to populate a component with data using the v-for directive

I am currently constructing a vue2 component, utilizing a vuex store object. The structure of the component is as follows: <template> <ul id="display"> <li v-for="item in sourceData()"> {{item.id}} </li ...

Circular Dependencies in Singletons within CommonJS Modules

I am pondering the possibility and method of achieving the following: In a CommonJS environment, using modules for both node and browser (with Browserify). I have two (or more) modules that each return a singleton object that needs to be accessed in diff ...

Creating a CSS dropdown menu - Placing a before element in front of the select/option element

How can I position a CSS :before element in front of the foreground content? I am trying to move the text behind the gray area where the dropdown arrow is located. I have already attempted to set the z-index of the before element to 2 and the actual eleme ...

Understanding the functionality of app.listen() and app.get() in the context of Express and Hapi

What is the best way to use only native modules in Node.js to recreate functionalities similar to app.listen() and app.get() using http module with a constructor? var app = function(opts) { this.token= opts.token } app.prototype.get = function(call ...

Optimal method for displaying the children component twice in Next.js

In order to create an infinite slider, I had to duplicate the data within my component. The data consists of an array containing a maximum of 20 items, each with an image. The slider is functioning perfectly. Although it may seem unconventional, it was n ...

Identify when 2 sets of radio buttons are chosen using jQuery

I need assistance with a webpage that presents the user with two simple yes-no inquiries. Below each question, there are two radio buttons for selecting either yes or no. <p>Question 1: Yes or No?</p> <input type="radio" name="q ...

Sliced Text Transformation

Recently, I inquired about a pesky text effect that was bothering me. Thanks to the assistance of fellow users, I discovered that the culprit behind the effect was the background color's edge. Eliminating this problem should be simple, but even with ...

Can an auto-margin container use an image as its left and right border using :before and :after pseudo-elements?

Hey there! I'm currently working on a project where I need to enclose a Youtube video in a container with automatic margins. The twist is that I want to add two images as a border on the left and right sides using the :before and :after pseudo-element ...

Mastering the art of calculating the width for a responsive scroll menu

I found a useful tutorial on building a scrolling menu for smaller screen sizes. You can check it out here. However, I encountered an issue when trying to add a border width element. Whenever I define the width, it overrides the scrolling: auto; element. ...

Assigning an identification number to specify the type of Chip

I am currently working on a project involving Material UI "Chips" that contain text and serve as references. Within the context of my project, I have Chips for both White Advantages and Black Advantages sections. However, there are instances where these Ch ...