mathiasbynens / jquery-placeholder

A jQuery plugin that enables HTML5 placeholder behavior for browsers that aren’t trying hard enough yet
https://mths.be/placeholder
MIT License
3.98k stars 1.34k forks source link

timeout has been increased in order to prevent submitting placeholder values #265

Closed stukalin closed 6 years ago

stukalin commented 9 years ago

I had an issue when placeholder values have been submitted. I increased the timeout slightly and it helped.

QWp6t commented 9 years ago

"slightly"

just a couple orders of magnitude. lol

jcrben commented 8 years ago

The current discussion seems to be at https://github.com/mathiasbynens/jquery-placeholder/issues/244

There was a discussion on this section of code at https://github.com/mathiasbynens/jquery-placeholder/commit/0db4369fdf2cefa87d81e55ef6dc586ba4eed6bb#comments where another person - back in 2012! - said that this same setTimeout was causing intermittent submission of the placeholder text to the server and that deleting it fixed it.

So basically there is a race condition in the submission and the setPlaceholder() firing. It seems like there should be a better way to work around this than a setTimeout (and maybe it could be shorter), but something is better than nothing.

:+1:

leven87 commented 7 years ago
    customClass: 'placeholder',
    hiddenOnfocus:true//if hide placeholder value when on focus for mordern browser
};

if (isInputSupported && isTextareaSupported) {
        placeholder = $.fn.placeholder = function (option) {
        settings = $.extend({},defaults,option);
        if(settings.hiddenOnfocus){
            return this.filter('[placeholder]')
                .bind({
                'focus.placeholder': clearPlaceholderInMordernBrowser, 
                'blur.placeholder': setPlaceholderInMordernBrowser
                })
                .data('placeholder-enabled', true)
                .trigger('blur.placeholder');
        }else{
            return this;
        }             
    };

    placeholder.input = true;
    placeholder.textarea = true;

    var clearPlaceholderInMordernBrowser = function(event){
        var input = this;
        var $input = $(this);
        if (input.value === "" && $input.hasClass(settings.customClass)) {
            $input.attr('placeholder',"");
            $input.removeClass(settings.customClass);
            input == safeActiveElement() && input.select();
        }

    };
    var setPlaceholderInMordernBrowser = function(event){
        var input = this;
        var $input = $(this);
        if($input.attr('placeholder') != ""){
            $input.data('placeholder-data', $input.attr('placeholder'));
        }
        if(input.value === ""){
            $input.addClass(settings.customClass);
            $input.attr('placeholder',$input.data('placeholder-data'));
        }else{
            $input.removeClass(settings.customClass);
        }
    };        

}