Updating the CSS class with JQuery Validation

I created a CSS class that triggers a transition effect when an input field is focused, and I used JQuery for input validation. The validation works smoothly, but now I want the transition effect to change color based on whether the input was valid or not.

This is the code for my transition class:

.bar::before {
  content: '';
  height: 0.125rem;
  width: 0;
  left: 50%;
  bottom: -0.0625rem;
  position: absolute;
  background: #337ab7;
  -webkit-transition: left 0.28s ease, width 0.28s ease;
  transition: left 0.28s ease, width 0.28s ease;
  z-index: 2;
}

Initially, I tried using .has-error, but it didn't work as expected:

.has-error .bar::before {
  background: #d9534f;
  left: 0;
  width: 100%;
}

I also experimented with input:invalid, but it only applies to HTML validation rather than the library's validation. Despite searching for solutions like in this example: , I couldn't find a suitable solution.

In essence, I aim to make the transition bar turn red when the field is invalid upon focus, while keeping the error messages intact.

Any assistance would be highly appreciated :)

Answer №1

Check out this solution that should meet your requirements: https://jsfiddle.net/ColiniloC/f9kshd1c/

HTML Code:

<label class='bar'>
    <input type='text' />
</label>

CSS Code:

.bar{
  display: inline-block;
  padding-bottom: 0.125rem;
  position: relative;
}

.bar::after{
    content: '';
    height: 0.125rem;
    width: 0;
    left: 50%;
    bottom: 0;
    position: absolute;
    background: #337ab7;
    -webkit-transition: left 0.28s ease, width 0.28s ease;
    transition: left 0.28s ease, width 0.28s ease;
    z-index: 2;
}

.bar.has-error::after{
  background: #d9534f;
  left: 0;
  width: 100%;
}

Key updates include:

  1. Make sure there is sufficient space within the container for .bar::after to avoid alignment issues.
  2. The bar should stay at the bottom of the container.

You may consider moving the red color styling to the default state of the bar depending on your desired effect.

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

Undo a CSS transition when clicked

There is a div named learn-more which expands to fill the whole page when a CSS class is added like this: .learn-more{ padding:10px; font-size: 20px; text-align: center; margin-top:20px; font-family: klight; -webkit-transition:2s; ...

The fixed positioning isn't functioning properly, here is the code snippet from my CSS and HTML

CSS Code I am not associated with any element that has the CSS property overflow: hidden. <div class="header"> <section class="top"> <div class="links"> &l ...

Simple math operations in GWT CssResource

I need help achieving a similar result to the following: // design.css @define borderThickness '2px'; .format { width: borderThickness + 2; height: borderThickness + 2; } The desired output is for the width and height properties to equal 4 ...

How can I implement number validation for a specific range with minimum and maximum values using <Input type="number">?

I am attempting to achieve the following goal: I require a basic input field that only accepts numbers between 5 and 10, including 0. `<input type="number" required ng-model="item.quantity" ng-min="5" ng-max="10"/>` The curre ...

Implementing a click event listener to target a specific div using JavaScript

In my JavaScript code, I have implemented a snowfall effect. I am looking to create a click event specifically on the 10th snowflake that falls down. Additionally, I want to add a class called "clickable" to this snowflake. The goal is to redirect the user ...

Extracting information from console output and displaying it in a table with angular2

https://i.stack.imgur.com/BMt6J.pngI am facing an issue with retrieving data from the console output and populating it into an HTML table. Can someone please assist me with this? Here is the HTML Code: <table class="table"> <tr> <t ...

Errored Out: No content found in file_get_contents('php://input') following an ajax post

I've run into a dilemma and am seeking help. I recently encountered an issue where data sent to a PHP file through an AJAX function seemed to vanish when attempting to retrieve it in the PHP file. Strangely enough, a similar function I implemented yes ...

Ensure a div stays on a single line when viewed in Internet Explorer

My current setup is structured in the following way. <div id="header"> <div id="heading">Title</div> <div id="flow"> Enter Something: <input type="textbox" size="30" id="id" class="class" onkeyup="dosomething()" /> &l ...

Choosing an element from a multiple group listbox with jQuery

Is there a way to select items in the listbox using jQuery when dealing with optgroup elements? $('#id option[value=<?php echo $row; ?>]').attr('selected','selected'); The above code works for regular options, but how ...

Optimizing button tab order with JQuery dialog tabindex

I have a modal window that includes two buttons created with jQuery UI. Within the modal, there are form elements that I can navigate using the TAB key. However, I am unable to reach the "Yes" button using the TAB key as it seems there is no TabIndex set. ...

How to properly position HTML elements and center text within paragraphs with inputs

Hello friends! I've been scouring Google and YouTube for a solution to my issue but no luck so far. This is the first time I've encountered this problem. I want to align everything in the center, which it is, but there seems to be a vertical ali ...

Determining the primary image in Galleriffic

After a recent inquiry, I am in the process of revamping a webpage to incorporate Galleriffic for optimized image loading, requiring me to delve deep into the intricacies of the task. I am encountering challenges in identifying the currently active image ...

Dynamically adjust input controls with ease using jQuery and Bootstrap

Upon reviewing the code provided in this link, I attempted to customize it by adding new dropdown lists to my form. However, the results were not as expected – alignment issues and incorrect labeling arose. Here is the updated code snippet: <div clas ...

Looking to enable form resubmission with Rails AJAX functionality

Creating an application where I need to implement the functionality of adding people to a department using AJAX. The issue I am facing is that while I can remove people from a department multiple times, I am unable to add them more than once. It appears th ...

What is the best way to adjust the size of the circles in my d3 leaflet implementation based on the frequency of longitude/latitude pairs?

I'm currently diving into the world of d3 and attempting to create a map using leaflet. Within my dataset, I have thousands of latitude and longitude coordinates. While I've successfully plotted circles on a leaflet map in d3, I'm now faced ...

Storing JSON data from a Github Gist in an array using the AJAX method - a step-by-step guide

I'm experimenting with storing JSON data in a variable and then randomly accessing it. (Superhero Alias Generator) The JSON data can be found on GitHub gist below I decided to try using the ajax method for the first time, but it doesn't seem to ...

Updating an Image Using JavaScript

Having trouble changing an image using JavaScript. Followed instructions to use document.getElementbyId("image").src = "image2.jpg", but it's not working. Script is placed at the bottom after the image tag in the HTML. HTML code: ...

What is the process for transforming a Microsoft Word HTML file into a streamlined XHTML document with inline styles?

Can someone help me with an issue I'm having regarding internal CSS? It seems that Jsoup is not able to parse the @font-face or similar elements. Any ideas on how I can clean up a Microsoft Word HTML document? ...

Information regarding the header on the client side

My web application is built on PHP and displays images from external servers. Some of these images, especially those from Flickr, may be deleted and leave behind a redirect to a "Missing Image" gif. To check if an image has been deleted server-side, I use ...

Can the floating side of a div be switched after resizing?

Let's say I have two divs set up in the following configuration: |------------------------------| | div1 ------------------ div2 | |------------------------------| .div1{ float:left; } .div2{ float:right; } From my understanding, they w ...