Temporary code implemented for IE8 is causing the dropdown login field to lose focus

I have encountered an issue with my code in IE8. Whenever I move the mouse around in the dropdown login field, it loses focus and disappears about 70% of the time. After debugging, I discovered that this problem is related to a certain placeholder code:

Update: Interestingly, the problem persists across various placeholder plugins I've tried. Removing these plugins resolves the issue.

$(document).ready(function() {

    if ( !("placeholder" in document.createElement("input")) ) {
        $("input[placeholder], textarea[placeholder]").each(function() {
            var val = $(this).attr("placeholder");
            if ( this.value == "" ) {
                this.value = val;
            }
            $(this).focus(function() {
                if ( this.value == val ) {
                    this.value = "";
                }
            }).blur(function() {
                if ( $.trim(this.value) == "" ) {
                    this.value = val;
                }
            })
        });

        // Clear default placeholder values on form submit
        $('form').submit(function() {
            $(this).find("input[placeholder], textarea[placeholder]").each(function() {
                if ( this.value == $(this).attr("placeholder") ) {
                    this.value = "";
                }
            });
        });
    }

});

An example can be viewed here:

Update: Unfortunately, I cannot test the fiddle link provided in IE8. Is there any solution to this problem?

Answer №1

Have you considered modifying your event handling to use 'mouseenter' and 'mouseleave' for the dropdown instead of 'focus' and 'blur'? This approach tends to be more consistent on older versions of Internet Explorer. It's also recommended to attach the event directly to the 'dropdown' div rather than the 'input' element.

To implement these changes, you can update your code as follows:

$(function() {
    var dropdown = $('div.login div.dropdown')
        .on('mouseenter', function() {
            dropdown.css('display', 'block');
        })
        .on('mouseleave', function() {
            dropdown.removeAttr('style');
        });
});

Answer №2

  • Example:
$(function(){
    $('#main_header .login li').hover(function(){
        $(this).find('.dropdown').show();
    },function(){
        $(this).find('.dropdown').hide();
    });
});
  • Please Note: I have also tidied up and corrected some coding issues in your JavaScript code...

Answer №3

This script is used to add a placeholder feature across all browsers by leveraging Modernizr for detection:

Check out the Demo here: http://jsfiddle.net/4D7Gt/

var placeholderOnBlur = function () {
    var input = $(this);

    if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
    }
};

var placeholderOnFocus = function () {
    var input = $(this);

    if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
    }
};

var placeholderOnSubmit = function () {
    $(this).find('[placeholder]').each(function () {
        var input = $(this);

        if (input.val() == input.attr('placeholder')) {
            input.val('');
        }
    });
};

var placeholderOnLoad = function () {
    if (!!$(this).attr('placeholder')) {
        $(this).on('focus', placeholderOnFocus);
        $(this).on('blur', placeholderOnBlur);
        $(this).parents('form').on('submit', placeholderOnSubmit);
        $(this).blur();
    }
};

 if (!Modernizr.input.placeholder) {
     $('[placeholder]').each(placeholderOnLoad);
 }

I haven't been able to test it on IE8, but theoretically it should function correctly.

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

Begin a new session in ASP.NET using C# and JSON

I am currently facing an issue in my ASP.NET C# project where I need to execute a Json by clicking on a tag, but this action ends the session. To resolve this, I have to call the method RestartSesion(). The code for this is located in the MasterPage. Howe ...

What is the process for utilizing an Angular service within a view expression?

Angular guidelines suggest utilizing Angular services and expressions: It is recommended to use services like $window and $location in functions called from expressions. These services offer a way to access global variables that can be easily mocked. - ...

Using Nginx to run both PHP and Node.js, with PHP files using the .php extension and Node.js files using the .html extension

Hello, I am trying to configure my server to handle PHP files and HTML files on the same server. Specifically, I want HTML files to be handled by Node.js on port 8080, while PHP files are handled by the PHP5 service. Here is the Nginx configuration I have ...

Using ng-class in Angular.js with a boolean retrieved from a service: A step-by-step guide

I need to dynamically set a class based on a boolean value that is determined in a service. This example is simplified for readability, but in reality, the boolean would be set by multiple functions within the service. Here is how it would look in HTML: ...

In React JS, you can't use the `map` function

I attempted to map out this response but encountered an error even after placing it in a usestate array. Please review my code to see if you can help me resolve the issue. function Comment() { const [comment_data, setcomment_data] = useState([]); u ...

Mastering intricate CSS customization

The situation I have a specific issue with CSS that I need help with. I am currently working on customizing the user interface of our Zimbra deployment, which uses jetty (a platform I am not very familiar with). I have come across two files that seem to b ...

Utilizing an SSL certification (pem file) within JavaScript

Currently in the process of developing a program to extract data from the FAA's AIDAP database. I received a security certificate in the form of a .p12 file, which I have successfully converted into a .pem file. However, I am encountering difficulties ...

Is it preferable to load JQuery before displaying the content of the HTML page?

My mobile site is set to 762px width, which is ideal for a PC screen. However, I am trying to style it to be 320px wide for an iPhone using JQuery. The issue I am facing is that when the page loads, it initially appears at the larger width and then resizes ...

Choose all pseudo elements from elements belonging to the class or ID labeled as 'x'

I am seeking a solution to target all the pseudo elements of an element that carries the class galleryBlock, as well as a method to target all the pseudo elements of an element with a specific id. My code structure consists of a grid made up of multiple i ...

Is there a way to bypass or neglect the row style class within a column?

When I applied rowStyleClass to my DataTable, it was based on the following conditions: <style> .deleted { color: darkgray; text-decoration: line-through; } .normal { } </style> However, I encountered an issue where the last t ...

Adjust the background color of the body content to reflect changing content

How can I change the background color to #97EFFC when a menu item is selected? I want the body content background to be transparent until a menu item is displayed. Site.css /* Responsive: Portrait tablets and up */ @media screen and (min-width: 768px) { ...

Changing the time zone of a UTC date string while preserving the original format

Is there a way to convert a UTC date string to the current user's timezone while keeping the original date string format intact? Take for example the following code snippet that accomplishes this: var data = '2017-04-24 12:06:37'; var date ...

Determine in JQuery whether an element is positioned at the top of the screen

My webpage has a div that is initially positioned approximately 100px from the top of the browser window. As the user scrolls down, I aim to create an effect where this div remains in place until it reaches the very top of the screen. At that point, I pl ...

Update a div with ajax

I am looking to refresh a select element (identified by its id) once the ajax call has been successfully completed. However, I am unsure of how to go about this. Ajax Function : $('#insertForm').on('click', function(){ var form_inti ...

How can I designate an object in an array with Vue.js?

Here is a sample response I received from an API call: { "1-2021": [ { "id": 1, "status": "New", "player_count": 7 }, ... Now, I need to iter ...

Encountering a NULL argument in an MVC controller when executing an Ajax post request

I am facing an issue with my ajax post request where I send JSON string data, but it is showing null in my controller. Here is the AJAX Call: var jsonData = JSON.stringify(matrixPresenter); $.post("/matrix/Savematrix?matrixData=" + jsonData , fu ...

What could be causing the images to not display on my HTML page?

My program is designed to display an image based on the result of the random function. Here is my HTML: <div> <h2>Player 0:</h2> <div id="MainPlayer0"></div> </div> Next, in my TypeScript fi ...

Encountering the issue of "unable to retrieve" when attempting to create an API in Express.js

I am currently developing a REST API using express js, but I encountered an issue with the error message "Cannot GET /api/bears". Here is my code snippet: server.js var express = require('express'); var app = express(); var bodyParser = require ...

Regular expressions are effective tools, but they may not be as functional within an

Trying to validate URLs using a regex has been tricky. The regex I have works perfectly fine on regex101.com, but for some reason it keeps failing validation when implemented in my Angular field. I'm at a loss as to how to tweak it so that Angular wil ...

Can I find a better approach to optimize this code?

How can I refactor this code to move the entire DB query logic into a separate file and only call the function in this current file? passport.use( new GoogleStrategy({ clientID: googleId, clientSecret: clientSecret, callbackURL: ...