Label animation on input form

I am in the process of developing a WordPress website and utilizing Contact Form 7 for building forms.

My goal is to implement a custom animation for the input labels. When there is no user interaction, the label is positioned inside the input field. Upon focusing on the input, the label smoothly slides up to make space for the input value. If the input field is left empty upon focus out, the label should slide back to its original position; otherwise, it should remain in place.

The animation itself is fairly straightforward and has been successfully implemented. However, I am encountering an issue with checking whether the input is empty or not. Below is the jQuery code snippet that I am currently using:

jQuery(document).ready(function($) {
    var cinput = $('.placeholder-animation input.animate-label');
    var clabel = $('.placeholder-animation span.custom-label');

    cinput.each(function() {
        if(cinput.val() === "") {
            console.log('Up!');
            cinput.focus(function() { clabel.addClass('up'); }).focusout(function() { clabel.removeClass('up'); });
        } else {
            clabel.addClass('up');
            console.log('Always up!');
        }
    });
})

Despite my efforts, I am facing a persisting issue where even after entering a value in the input field, the label incorrectly slides back as if the input was still empty. I have tried various solutions but none have yielded positive results...

Any assistance would be greatly appreciated! Thank you in advance!

Answer №1

You have the ability to accomplish this using CSS3 exclusively.

To achieve this, place the input inside the label and add a span after the input. Then utilize the :focus selector:

input:focus + span {
 /*label css here*/
}

UPDATE: Take a look at this demonstration on CodePen: https://codepen.io/MaxViewUp/pen/xrbdxm

If you find it challenging to implement this with solely CSS (perhaps due to a wrapper around the field because of a plugin), you can resort to jQuery for adding/removing classes. In such cases, just add the class selector to the active label.

UPDATE: Additionally, you will need some JavaScript code to detect when the field is filled (until CSS enhancements are made). Make sure to check out the updated pen now.

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

Tips for Locating an Element by Its Attribute in JQuery

I currently have a variable that holds a list of selects, and my goal is to filter out only those with a 'dependsOn' attribute set to 'EventType'. My initial attempt below doesn't seem to work as intended and returns a count of 0: ...

Creating a new class in Laravel and initializing it using jQuery and AJAX: A Guide

I am a newcomer to Laravel and I would like to create a class in Laravel that contains a static attribute for use in views and controllers: I have the following class in mind: class Info { static $id; static $temps; public function setI ...

Smooth-scrolling feature with touch interaction available in the scrollbar extension

Anyone here aware of a high-quality jQuery or CSS plugin for scrollbars that supports touch functionality and smooth easing? I've been on the lookout for one, but haven't had any luck so far. Even if it comes with a price tag, I'm interested ...

Increase the font size of the text inside a table cell without altering the cell's width

Let me clarify my query with some images. This is how my current setup looks: https://i.sstatic.net/tyelv.png Everything is contained within a table, where the boxes span the width of the table. However, I need the data field to expand across the full sc ...

Is there a way to integrate the AJAX response from JavaScript into a JavaScript function?

I am having a nightmare trying to solve this issue. I am currently working on an app using phonegap and have integrated highcharts for creating graphs. The graph displays properly, but the tooltip is not working as expected. Below is the code snippet that ...

Is it possible to achieve a height transition using CSS instead of relying on JavaScript

I created these custom cards using a combination of HTML, CSS, and JavaScript. However, I've noticed that the height transition animation is not as smooth as I'd like it to be, especially on certain pages. Is there a way to achieve this animation ...

Focused Filtering in DataGrid Pagination

Seeking to adjust the font size of numerical values (10, 25, and 50 as shown in the screenshot below) within rows per page selection within a pagination section of a DataGrid component. https://i.sstatic.net/PnIDa.png After inspecting each number, it was ...

Showing an incorrect rendering appearance, the styled <select> element in Bootstrap4 is not

I've incorporated Bootstrap into my website and noticed that the <select> elements look strange without any extra styling. I'm particularly concerned about the appearance of their arrows. Here's how a <select> element styled wit ...

:last-child is effective, but :first-child is inefficient

I'm having trouble using CSS to hide the first .sku element within an aside that contains two of these elements. Strangely, the :last-child selector works just fine. Can anyone explain why this might be happening? Here's a link to the JSFiddle fo ...

Is there a JavaScript library available that facilitates the seamless synchronization of data between clients and a node.js server?

Are there any JavaScript libraries available that can synchronize a specific set of data stored on a server, such as an array, across multiple clients automatically? ...

Which is better for creating an interactive map: Jquery or HTML5?

Hey everyone! I'm looking to develop a zoomable interactive map similar to the one on this cool website Any recommendations on whether Jquery or HTML5 would be best for creating this kind of map, and why? Your expertise would be greatly appreciated! ...

Ways to modify the text of an HTML element when hovering over it

One of my elements contains the text &times; within a <span>. Is there a way to change this symbol to something else, like &infin;, when hovered over? <span class="change-text">&times;</span> When the mouse hovers over it, I ...

Tips for adjusting webpage layout automatically based on window dimensions?

As someone who is new to web development, I am in search of a simple solution that allows my developing page to dynamically resize according to the window size without disrupting the layout. Additionally, I am trying to center the page, but experiencing is ...

Guide on using CSS to position text around an image

I need to figure out a way to wrap text around an image positioned in the bottom left corner of a CSS table. Since the amount of text may vary each time I use it, I am wondering if there is a solution for this and how I can achieve it? ...

Setting a div to occupy the entire height of a webpage using HTML and CSS

Is there a way to make the div (the blue box) fill the entire page? I tried setting the body tag to height:100%, but it didn't work. You can view an example at http://jsfiddle.net/rVyrX/1/ ...

Assigning data attributes to jQuery objects using the script type of "text/template"

I have inserted the following snippet in my HTML file: <script id="tpl" type="text/template"> <div data-id="" class="line"> ... </div> </script> Within my JavaScript code, I am trying to retrieve this template, add it to t ...

Difficulty with HTML Bootstrap 3 including active class in UL LI elements

Struggling to implement scroll-spy for adding an active class to the ul li elements on a one-page scroll with a sticky nav. For some reason, I can't seem to get it to work within my project. Below is the menu structure: <body id="page-top&quo ...

Why does `overflow-wrap: break-words` behave differently from `word-break: break-words` and which one is more effective in certain situations?

What is the distinction between overflow-wrap: break-words and word-break: break-words? While many sources claim that there is no difference, my personal experiments suggest otherwise: <!-- truly breaks words --> <table style="width: 100px; ...

Target the CSS element with a specific class only if it is not the first child within its parent

In my coding project, I have created a div.container which contains multiple p elements. Some of these p elements have the class p.special. My goal is to select the p.special elements that are not positioned at the very top of the parent container (i.e., n ...

extlib dynamic content control causes chaos

Encountering a strange issue with xpages that seems to be related to the dynamic content control. This problem has cropped up before in different scenarios, but the cause and solution have always eluded me. In this particular instance, I have a dynamic c ...