What is the best way to choose the closest <input> element?

How can I achieve an interactive icon effect within an input field? Specifically, when hovering over the icon, I want it to change and also alter the "type" attribute of the targeted input field among three similar fields.

Here's the HTML structure:

<div class="change_subtitle">Enter Password</div>
<input id="pw_old" class="change_input" type="text"><i class="fa fa-eye-slash" aria-hidden="true"></i></input>
<div class="change_subtitle">Enter new Password</div>
<input id="pw_new1" class="change_input" type="password"><i class="fa fa-eye-slash" aria-hidden="true"></i></input>
<div class="change_subtitle">Confirm new Password</div>
<input id="pw_new2" class="change_input" type="password"><i class="fa fa-eye-slash" aria-hidden="true"></i></input>
<div class="button_confirm" onclick="inputIsValid()">Confirm</div>

The jQuery implementation for this functionality is as follows:

$(".fa-eye-slash").hover(function(){
   if($(this).hasClass('fa-eye-slash')) {
       $(this).removeClass('fa-eye-slash');
       $(this).addClass('fa-eye');
       $(this).attr('type' = 'text'); // Update the input field type
   } else if($(this).hasClass('fa-eye')) {
       $(this).removeClass('fa-eye');
       $(this).addClass('fa-eye-slash');
       $(this).attr('type' = 'password'); // Revert back to password type
   }
})

To further understand how the icon relates to the inputs, here is a snippet of the CSS code:

.fa-eye-slash{
    color: #34495e;
    margin-left: -20px;
}

.fa-eye{
    color: #34495e;
    margin-left: -20px;
}

When interacting with the icon, it should dynamically switch the input field type between "password" and "text".

Answer №1

For a solution, you could check out this fiddle path: https://jsfiddle.net/afckhbpo/

The issue lies with

$(this).attr('type' = 'text') 

To fix it, try using:

$(this).prev().attr('type', 'text')

Answer №2

Regarding the query in your heading, considering that these symbols are i elements positioned right after the relevant field, using $(this).prev() will provide a jQuery object for the corresponding input.

Therefore, when I hover over the icon, it should change and transform the input field type from "password" to "text" and vice versa later.

Once an input is established with a certain type, altering that type is not allowed according to the specification, although some browsers do not support this feature yet. As an alternative, you can work with two separate inputs (one set to type="text" and the other to type="password") and display or hide them as needed.

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

JQuery for personalized path animations

After developing an android app that sends onTouchEvents points to a web server, I've been able to retrieve motion points JSON data using Ajax. Here is a snippet of the data: {"data":[ {"x":224.28035,"y":235.4906}, {"x":263.32916,"y":219.45718}, {"x" ...

Try rotating a triangle in 3D using either CSS or JavaScript

I want to create a right-angle triangle that looks like this . . . . . . . . . . . . . . . . . . but I also want to add a 3D animation that will transform it into a right angle like this . . . . . ...

What is the best way to adjust inline svg to the user viewport size?

After working on integrating my interactive SVG maps into my HTML pages, I encountered a minor issue: when inserting my inline SVG into a standard, non-responsive HTML document, it automatically adjusts itself to fit nicely within the user's viewport. ...

Display a message on the webpage itself, not in a pop-up, when the value exceeds 1 using JavaScript

I need help with a condition involving a variable called "truecount." If this value is less than one, I would like to display an error message on the screen instead of an alert popup. Could someone assist me with this? I am new to this and don't have ...

Is there a discrepancy in performance when running a function on an individual element versus a group of elements within jQuery?

Imagine having the choice between applying a function to an individual DOM element or a list of them: For Individual Elements: $('#element1').click(function () { $(this).hide(); return false; }); $('#element2').click(functi ...

Modify the hue of a background image in PNG format

I'm currently using a CSS selector on my site to display an icon for external links on any href that isn't directing to my own domain. Here's the code snippet I'm referring to: a[class=" external-link"]::after { content: url(data:im ...

Issues with uploading files

I am trying to implement a functionality where users can upload video, audio, images, PDFs, PowerPoint presentations, and Word files, and then have them saved into a directory. However, when I try to upload the files using my code, there are no errors but ...

jquery code to show/hide all elements

My webpage contains multiple content tabs with expand/collapse icons in each panel. To simplify the process of closing all expanded sections, I have included buttons for expanding and collapsing all panels at once. While this feature is functioning correc ...

Angular: The application has encountered an error due to an undefined property reading issue, resulting in a type error

Here is the code for the company.ts file: company.ts file - List item export class Company{ gstNo:number; contactNumber:number; contactName:string; phoneNo:number; address:string; companyName:string; } import { Injectable } ...

Rotating picture panel featuring an assortment of images

I'm working on a responsive image grid and looking to create a unique design. Instead of repeating the same image in each box, I would like to implement a mask that shows the image across all boxes, similar to this: Additionally, I want to add a flip ...

What could be causing the significant gap at the bottom of my Flexbox?

Whenever I resize the page to fit a mobile device, there is always a large gap at the bottom. I want the content to stack normally when it shrinks, without leaving a huge gap at the bottom. There is no gap at the bottom when viewed on desktop, but as soo ...

The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual re ...

CLS notifies about an Unrecognized CSS Element in Page Insights' Core Web Performance

My experience with Google PageSpeed Insights has been quite frustrating due to the numerous alerts I'm receiving about Unsupported CSS Properties. The importance of avoiding non-composited animations: Animations that are not composited can appear gli ...

Calculator built with HTML, CSS, and JavaScript

Hi there, I'm experiencing some issues with my calculator. The buttons seem to be working fine and lining up correctly, but for some reason, nothing is showing up on the monitor or getting calculated when I press the buttons. Here's the code that ...

Implementing a dynamic loading strategy for Google reCAPTCHA based on language selection

I have a unique application that requires the selection of one language out of four options (English, French, Dutch, español) in a form. Below the language selection, the Google reCaptcha is displayed. I am looking to dynamically load the reCaptcha scrip ...

Notification triggered from the controller

I am currently developing a user management application in which I have to add a user to the database. When a button is clicked on the client screen (index.cshtml), a JQuery dialog box will appear allowing users to fill in the necessary details (AddUser.c ...

Creating input fields using Bootstrap HTML on a single line

Struggling with Bootstrap, I can't figure out how to display the following HTML inputs in a single line: <div class="row"> <div class="col-sm-8"> <div class="form-group"> <input class="form-control" ...

Example of Utilizing Google Places API

The Issue I've been struggling to make this google maps API example function correctly. Even after directly copying the code from Google's website, the example fails to display properly. For instance, the map doesn't show up, the search bar ...

Is it feasible to utilize the .fadeTo() function in jQuery multiple times?

I need some help with writing a script that will display a warning message in a div tag every time a specific button is pressed. I chose to use the fadeTo method because my div tag is within a table and I want to avoid it collapsing. However, I'm noti ...

execute the function whenever the variable undergoes a change

<script> function updateVariable(value){ document.getElementById("demo").innerHTML=value; } </script> Script to update variable on click <?php $numbers=array(0,1,2,3,4,5); $count=sizeof($numbers); echo'<div class="navbox"> ...