Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested).

Here is the CSS snippet :

$(document).ready(function(){
$(".placeholder").click(function(){
$(this).addClass("placeholder-small");
$(this).prev("input").focus();
});
$("input").on("focus",function(){
$(this).nextAll(".placeholder").first().addClass("placeholder-small");
});
});
* {box-sizing:border-box;outline:none;-webkit-appearance:none}
html, body {padding:0;margin:0;background-color:#131419;color:#fff;font-family:"DIN", arial;font-size:14px;letter-spacing:0.15em;font-weight:300}
input[type="text"], input[type="email"], input[type=number], input[type="password"] {max-width:250px;width:100%;background-color:transparent;border-radius:0;border:0;border-bottom:1px solid #b6b6b8;color:#fff;font-family:"DIN",arial;font-size:inherit;padding:10px 8px;margin:10px 0;z-index:1}
.required {position:relative}
.required:after {position:absolute;top:0;right:0;content:"*";color:#ff0000;z-index:9}
.placeholder {position:absolute;top:0;left:0;transition:all .2s;z-index:0}
textarea ~ .placeholder {top:-135px}
.placeholder-small, input:-webkit-autofill ~ .placeholder {transform:translateY(-125%);-moz-transform: translateY(-125%);font-size:0.85em;color:#616777}
<form method="post">
<div style="padding:50px"><span class="required"><input type="text" name="firstname" value="" pattern=".{1,}" required minlength="1" maxlength="255" /><div class="placeholder">Prénom</div></span></div>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$(".placeholder").click(function(){
$(this).addClass("placeholder-small");
$(this).prev("input").focus();
});
$("input").on("focus",function(){
$(this).nextAll(".placeholder").first().addClass("placeholder-small");
});
});
</script>

If you'd like to see it in action, visit this page :

Answer №1

Here is the issue you are facing:

.placeholder-small, input:-webkit-autofill ~ .placeholder { ...

The specific webkit declaration is causing parsing errors on Firefox. If the parser encounters something it cannot understand, it will ignore the entire code block:

The selector includes everything up to (but not including) the first left curly brace ({). A selector always comes with a {}-block. When a user agent cannot parse the selector (i.e., it is not valid CSS3), it must also ignore the {}-block.

If you either remove the webkit part or separate it into its own statement, the code will function correctly:

.placeholder-small {
    transform: translateY(-125%);
    font-size: 0.85em;
    color: #616777;
}

input:-webkit-autofill ~ .placeholder {
    transform: translateY(-125%);
    font-size: 0.85em;
    color: #616777;
}

It is worth mentioning that you no longer need to use -moz-transform, as the standard transform has been sufficient since version 16 released in 2012.

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 increasing the height of a Kendo-UI grid to 100% within an AngularJS 1.x environment

Encountering an issue, I needed a Kendo UI Grid widget with its height set to 100% of the surrounding <div>. Attempting basic CSS styling on the grid proved unsuccessful: html, body { height:100%; } .expand-vertical { height: 100%; min-h ...

determining CSS selector

Having trouble identifying a locator using CSS. There are 10 elements with the same locator name on the webpage. Each web element has the same XPath value: //div[@class='thumb_image'] The web element list size is 10. If I want to access the 5t ...

As the page is scrolled upwards, the custom cursor's location shifts accordingly

In developing a custom hover cursor for my video div, I am encountering an issue where the cursor does not move with my mouse and remains in place when I scroll. Could someone please review my code and identify what I am doing wrong? Additionally, I have ...

Use jQuery to smoothly scroll to the top of a DIV that is currently being opened

One issue I am facing is with an accordion that opens and closes upon clicking. The problem lies in the fact that when I click on it, I want the page to scroll to the top of the div. However, my current code is not quite achieving this - it scrolls to wher ...

Want to analyze the response time of jQuery AJAX and initiate specific actions based on the results

$.ajax({ url: "http://ajaxhttpheaders.appspot.com", dataType: 'jsonp', success: function(headers) { language = headers['Accept-Language']; alert(language); }, //it& ...

Firefox browser does not display flashing titles for tabs

Every second, I want to display "New message..." in the title when the browser tab is inactive or the user is in another tab. Here's the code I used: <script> var mytimer; function log() { document.title = document.title == "" ...

Safari experiencing a CSS issue not found in Chrome or Firefox

https://gist.github.com/2354116 When you check the link above in Chrome or Firefox, everything looks good. The divs at the bottom, including the headings and social icons, are centered within a container div without any issues. However, when viewed in Sa ...

Display the menu upon activating the hover event icon using only CSS

Assistance Needed - Unable to activate hover and display the rest of the div I want to activate the hover effect where early levels are visible, and when hovering over the first level, the subsequent levels appear List item body { margin: 3%; } div.ti ...

retrieving the AJAX response from a PHP CodeIgniter API

My issue is that I am currently working on developing a Codeigniter API. This framework is new to me, and it's my first time building an API. To get started, I followed a simple tutorial to create a basic API with the following controller: /** * @rou ...

Tips for setting the style of child components in a ReactJS project with CSS modules, classNames, and JSX syntax

Currently in the process of setting up a project using React JS and I am interested in incorporating auth0 for authentication purposes, along with potentially integrating some serverless functions. auth0 conveniently offers a pre-made "seed" project at ht ...

Prevent the underlining of the :before content of a link from being affected by a rule applied to the link

I have a hyperlink that becomes underlined when hovered over. I want to add a > symbol before the link, but I don't want it to be underlined when the link is hovered. Here's the code I'm using: .box.blueb a { color: #0098aa; } .box.blueb ...

Whenever I select a menu item, my intention is to make all the other main menu options disappear

I've been grappling with this issue for quite some time now. My goal is to hide all other main menus whenever I click on one. For instance, if I click on the Discord menu, I want all other menus to disappear except for the sub-menu. Check out the cod ...

Using JQuery's ajax() method within a for loop

I'm currently working on implementing default edit mode in a Razor view. Everything seems to be functioning properly except for the filling function for the dropdown list. As part of my task, I need to populate the state dropdown list based on the cur ...

What is the process for embedding images and text within a nested division element?

I have a website layout with 4 main sections: header, left side, right side, and footer. I would like to further divide the header into two sections - left header and right header. The right header should contain an image along with other options like home ...

I've encountered a peculiar error that is new to me while using bootstrap and javascript. Can anyone shed light on what this error signifies?

Hey there! I've come across this error in the console while attempting to utilize Bootstrap. It seems that a style is being refused from 'http://127.0.0.1:5500/node_modules/font-awesome/css/font-awesome.min.css' due to its MIME type ('t ...

What are the steps to create a unique popup div effect with jQuery?

Upon clicking the icon on this page, a mysterious div appears with information. I'm completely baffled by how they achieved this cool effect using css/jQuery tools. Can anyone shed some light on the mechanism behind this? ...

Creating a unique look for SELECT (Option) tags using gradients and a personalized arrow design

Feel free to check out my full HTML code here. You can simply copy and paste it into a demo document on your local machine to see exactly what I'm talking about. My goal is to style the boxes to include both a gradient background AND a unique arrow o ...

Load JavaScripts asynchronously with the function getScripts

Asynchronously loading scripts on my login page: $.when( $.getScript("/Scripts/View/scroll-sneak.js"), $.getScript("/Scripts/kendo/kendo.custom.min.js"), $.Deferred(function (defer ...

Determine the height of an image using JavaScript

How can I retrieve the height of an image in a JavaScript function? When I use the code: var image_height = $(image).height(); The value of image_height is 0, even though my image definitely has non-zero height. Is there a different method to accurately ...

Utilize the JQuery range slider within an ASP.NET MVC Razor project

After finding an answer to a question on creating a simple range slider in ASP.NET MVC 3, I decided to implement a jQuery range slider for my ASP.NET MVC Razor view website. Although I managed to get the slider working on the frontend, I encountered issues ...