Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder code used for IE8 causing dropdown login field to lose focus

I am using the below placeholder code for IE8, however about 70% of the time when you move your mouse around in the dropdown login field it loses focus (the whole dropdown login field vanishes); through debugging - when I remove this code the problem goes away - I have found the cause of the problem is this code:

Edit: I have found it isn't caused by any particular placeholder code, but it IS caused by some part of the process as I have tried 3 separate placeholder plugins and it happens on all 3 of them; take them away and no problems.

$(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 = "";
                }
            });
        });
    }

});

You can view an example here: http://condorstudios.com/stuff/temp/so/header-sample.php

Edit: Not sure if it will help as jsfiddle doesn't work on IE8 and I can't test if the fiddle behaves badly in IE8 too, but here is the fiddle: http://jsfiddle.net/m8arw/7/

Any way to fix this?

like image 948
Brett Avatar asked Mar 09 '14 18:03

Brett


2 Answers

Have you tried switching your event to show/hide dropdown to 'mouseenter' and 'mouseleave'?
It's a lot more reliable on old IE than 'focus' and 'blur' event. Also, bind the event directly on the 'dropdown' div is more preferable than on the 'input' element.

In short, please try change this part of your code like this.

$(function() {
    var dropdown = $('div.login div.dropdown')
        .on('mouseenter', function() {
            dropdown.css('display', 'block');
        })
        .on('mouseleave', function() {
            dropdown.removeAttr('style');
        });
});
like image 114
Montri M Avatar answered Nov 15 '22 20:11

Montri M


  • demo: http://so.devilmaycode.it/placeholder-code-used-for-ie8-causing-dropdown-login-field-to-lose-focus
$(function(){
    $('#main_header .login li').hover(function(){
        $(this).find('.dropdown').show();
    },function(){
        $(this).find('.dropdown').hide();
    });
});
  • NOTE: i have also cleaned up and fixed some coding horror in your js code...
like image 23
Luca Filosofi Avatar answered Nov 15 '22 20:11

Luca Filosofi