Eliminate the star symbol - fixing the mishap with hasClass and removeClass in jQuery

Help needed! I'm trying to remove the asterisk class from the password field, but there seems to be a typo in my code. Can someone assist me with this issue? I've attempted to remove attributes, but that hasn't been successful either. I don't want both the eye icon and the asterisk displayed together as it doesn't look good, but the password field should still be required. Here is the HTML, JS, CSS code snippet:

var passField = $('.password');

        $('.show-pass').hover(function() {
                passField.attr('type', 'text');
        }, function() {
                passField.attr('type', 'password');
        });
    // Add Asterisk on Required Field

    $('input').each(function() {
        if($(this).attr('required') === 'required') {

            $(this).after('<span class="asterisk">*</span>');

        } else if ($(this).hasClass('password')) {
            
            $(this).removeAttr('span.asterisk');
        }
  
    });
.show-pass {
position: absolute;
top: 20px;
right: 15px;
cursor: pointer;
}
.asterisk {
font-size: 30px;
position: absolute;
right: 20px;
top: 14px;
color: #00704a;
}
.form-signin {
width: 100%;
max-width: 400px;
padding: 15px;
margin: auto;
}

.form-signin .checkbox {
font-weight: 400;
}

.form-signin .form-floating:focus-within {
z-index: 2;
}

.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}

.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8aaa7a7bcbbbcbaa9b888fde6f8e6f8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<form class="login mt-0" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

<div class="form-floating">
    <input type="username" name="username" class="form-control" placeholder="Username for Login" required>
    <label>Username</label>
</div>

<div class="form-floating password">
    <input type="password" name="password" class="password form-control" placeholder="******" required>
    <label>Password</label>
    <i class="fas fa-eye show-pass"></i>
</div>

<div>
    <input type="submit" class="sbxBgPrimary w-100 btn btn-lg btn-primary" value="Login">
</div>

Answer №1

When determining which inputs should have an asterisk added to them, password inputs can be excluded:

$('input:not(.password)').each(

The loop is not necessary as jQuery will automatically apply to all elements in the collection, resulting in:

$('input[required]:not(.password)').after('<span class="asterisk">*</span>');

Here is the updated code snippet:

var passField = $('.password');

$('.show-pass').hover(function() {
  passField.attr('type', 'text');
}, function() {
  passField.attr('type', 'password');
});

// Add Asterisk on Required Field

$('input[required]:not(.password)').after('<span class="asterisk">*</span>');
.show-pass {
  position: absolute;
  top: 20px;
  right: 15px;
  cursor: pointer;
}

.asterisk {
  font-size: 30px;
  position: absolute;
  right: 20px;
  top: 14px;
  color: #00704a;
}

.form-signin {
  width: 100%;
  max-width: 400px;
  padding: 15px;
  margin: auto;
}

.form-signin .checkbox {
  font-weight: 400;
}

.form-signin .form-floating:focus-within {
  z-index: 2;
}

.form-signin input[type="email"] {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}

.form-signin input[type="password"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4969b9b808780869584b4c1dac4dac4">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

<form class="login mt-0" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">

<br/><br/>

  <div class="form-floating">
    <input type="username" name="username" class="form-control" placeholder="Username to Log In" required>
    <label>Username</label>
  </div>

  <div class="form-floating password">
    <input type="password" name="password" class="password form-control" placeholder="******" required>
    <label>Password</label>
    <i class="fas fa-eye show-pass"></i>
  </div>

  <div>
    <input type="submit" class="sbxBgPrimary w-100 btn btn-lg btn-primary" value="Log In">
  </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

Concealing IP addresses within a JavaScript program

I'm currently developing a JavaScript application and I've implemented a method to hide IP addresses on the client side using includes and requires. Here's how it works: First, in includes.php: require ('C:/Program Files/Apache/Apache ...

Using jQuery to modify the border of a particular row

I am facing an issue where I need to apply a border to two specific rows in a table after it has loaded. These two rows are consistent every time the table is loaded. The code snippet below works, but the default line breaks between the table rows prevent ...

WordPress is failing to deliver a response from AJAX

I have a button in my template that should display 'Next is clicked' when pressed. However, after making a post request I am only seeing the first message and not 'Got response!' in the console. The following code is from my js/inputti ...

Accessing a google doc without signing in as a user

As I embark on the journey to create a simple webpage that can load text from a Google doc, I face the challenge of ensuring that the page operates solely on the client side without the need for any server interaction, since it is hosted on GitHub. User lo ...

Include the referrer header when using the chrome.downloads API

I have been working on developing an extension that replicates the Ctrl+Click to save image functionality from Opera 12. In my extension, I am utilizing chrome.downloads.download() in the background script to trigger downloads, while a content script is re ...

Struggling to display flex items in a full-width row on a new line

Here is a code snippet where I am trying to display "0 of 2 completed" on a new line at full width. I have used flexbox for this purpose, but I am facing challenges. Can you suggest a better approach to achieve this? .container { display: flex; widt ...

Hexagonal design table pillars

Can the top columns of a table be styled with column names in this manner? https://i.sstatic.net/rUbyA.png This is the code snippet I am using: <table class="" style="background: rgba(255,255,255,0.5); font-size:15px; border-collapse:separate; ...

Intrigued by the prospect of making HTML elements vanish or smoothly glide along with the content without obstructing it

I'm a beginner in coding and I want to make my website impressive. Right now, I have an HTML embedded element called "On this Page" that I would like to disappear when the user scrolls down the page. I've managed to position the element on the ...

Tallying responses of "Yes" and "No" in a React form and storing them

I'm currently working on a React form using Material UI components. To keep track of the responses, I have an empty array called questionAns. My goal is to append an element like yes to the array when the Yes radio button is selected in the form. Belo ...

Looking for Efficiency in Basic JavaScript Arithmetic Operations

I'm having trouble figuring out how to display a total price after a selection is made. My goal is to take the selected value, multiply it by 100, and then use the updateTotal function to show the total. // Gather Data & Prices for Updating Dynamic P ...

Struggling to convert my GRAPHQL response into a JSON object that can be iterated through

Here's a little context: I'm currently working with Shopify's GraphQL API and making calls to retrieve data. The data retrieval process is successful, however, I'm facing a challenge in manipulating the data using regular JSON syntax. M ...

The font remains the same despite the <Div style=> tag

I have a script that loads external HTML files, but I am facing an issue with changing the font to Arial. The script is as follows: <script type="text/javascript"> $(document).ready(function(){ $("#page1").click(function(){ ...

Sending requests to the incorrect endpoint in production, yet functioning flawlessly in the development environment

It seems that my application is directing to the wrong endpoint on the production environment, whereas it works perfectly fine on development. I have developed the backend using Express, frontend with React, and authentication with Passport. The issue aris ...

Create custom sign board buttons using CSS styling

Check out the button design linked below: I attempted to recreate this design using before and after, but I want the arrow to be softer. I tried skewing it as well. I also searched for references, but couldn't find similar examples online even though ...

Transferring information to the background event object within FullCalendar

When a user clicks on a background event, is there a way to retrieve the data that was passed to eventObj? Below is an example of what my sample background event looks like: { id: 'availableForMeeting', title: 'Test Background Event #1&ap ...

What is the best way to make a canvas element always follow the movement of the mouse cursor

Currently, I am delving into the world of HTML5 canvas and experimenting with a program that rotates a triangle to face the location of the mouse pointer. The functionality is mostly there, but it seems to skip half of the intended rotation. I am curious i ...

The XMLHttpRequest is unable to load.... The origin null has been restricted by Access-Control-Allow-Origin

I'm having trouble printing a JSON from a broker on my server. I am unable to return the JSON as Chrome gives me the following error: XMLHttpRequest can not load .... Origin null is not allowed by Access-Control-Allow-Origin. Can anyone help me wi ...

What is the best way to link this to a function in AngularIO's Observable::subscribe method?

Many examples use the Observable.subscribe() function in AngularIO. However, I have only seen anonymous functions being used like this: bar().subscribe(data => this.data = data, ...); When I try to use a function from the same class like this: update ...

The width property in HTML's select element is failing to be set at 100% reach

<div class="tabbable"> <ul class="mb10 nav nav-pills nav-justified form-tabs hidden-xs hidden-sm hidden-md hidden-lg"> <li class="tab-selector active"><a href="#tab1" data-toggle="tab">Information Services</a></li ...

Enhancing Material UI List Items with Custom Styling in React.js

My website's navigation bar is created using material-ui components, specifically the ListItem component. To style each item when selected, I added the following CSS code: <List> {routes.map(route => ( <Link to={route.path} key={ro ...