Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6237407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:01:24+00:00 2026-05-24T11:01:24+00:00

using the below jquery placeholder plugin that creates labels and overlays the field with

  • 0

using the below jquery placeholder plugin that creates labels and overlays the field with the label.

My issue is that I have field validation and a function that places focus on the field that returns a validation error.

By setting the focus programatically, the label/placeholder persists. If I click into the field, it works as it should.

Any obvious workarounds?

/*****************************************************************************
jQuery Placeholder 1.1.1

Copyright (c) 2010 Michael J. Ryan (http://tracker1.info/)

Dual licensed under the MIT and GPL licenses:
    http://www.opensource.org/licenses/mit-license.php
    http://www.gnu.org/licenses/gpl.html

------------------------------------------------------------------------------

Sets up a watermark for inputted fields... this will create a LABEL.watermark
tag immediately following the input tag, the positioning will be set absolute,
and it will be positioned to match the input tag.

To activate on all tags with a 'data-watermark' attribute:

    $('input[placeholder],textarea[placeholder]').placeholder();


To style the tags as appropriate (you'll want to make sure the font matches):

    label.placeholder {
        cursor: text;               <--- display a cursor to match the text input

        padding: 4px 4px 4px 4px;   <--- this should match the border+padding
                                            for the input field(s)
        color: #999999;             <--- this will display as faded
    }

You'll also want to have the color set for browsers with native support
    input:placeholder, textarea:placeholder {
        color: #999999;
    }
    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
        color: #999999;
    }

------------------------------------------------------------------------------

Thanks to...
    http://www.alistapart.com/articles/makingcompactformsmoreaccessible
    http://plugins.jquery.com/project/overlabel

    This works similar to the overlabel, but creates the actual label tag
    based on a data-watermark attribute on the input tag, instead of
    relying on the markup to provide it.

*****************************************************************************/
(function($){

    var ph = "PLACEHOLDER-INPUT";
    var phl = "PLACEHOLDER-LABEL";
    var boundEvents = false;
    var default_options = {
        labelClass: 'placeholder'
    };

    //check for native support for placeholder attribute, if so stub methods and return
    var input = document.createElement("input");
    if ('placeholder' in input) {
        $.fn.placeholder = $.fn.unplaceholder = function(){}; //empty function
        delete input; //cleanup IE memory
        return;
    };
    delete input;

    $.fn.placeholder = function(options) {
        bindEvents();

        var opts = $.extend(default_options, options)

        this.each(function(){
            var rnd=Math.random().toString(32).replace(/\./,'')
                ,input=$(this)
                ,label=$('<label style="position:absolute;display:none;top:0;left:0;"></label>');

            if (!input.attr('placeholder') || input.data(ph) === ph) return; //already watermarked

            //make sure the input tag has an ID assigned, if not, assign one.
            if (!input.attr('id')) input.attr('id') = 'input_' + rnd;

            label   .attr('id',input.attr('id') + "_placeholder")
                    .data(ph, '#' + input.attr('id'))   //reference to the input tag
                    .attr('for',input.attr('id'))
                    .addClass(opts.labelClass)
                    .addClass(opts.labelClass + '-for-' + this.tagName.toLowerCase()) //ex: watermark-for-textarea
                    .addClass(phl)
                    .text(input.attr('placeholder'));

            input
                .data(phl, '#' + label.attr('id'))  //set a reference to the label
                .data(ph,ph)        //set that the field is watermarked
                .addClass(ph)       //add the watermark class
                .after(label);      //add the label field to the page

            //setup overlay
            itemIn.call(this);
            itemOut.call(this);
        });
    };

    $.fn.unplaceholder = function(){
        this.each(function(){
            var input=$(this),
                label=$(input.data(phl));

            if (input.data(ph) !== ph) return;

            label.remove();
            input.removeData(ph).removeData(phl).removeClass(ph);
        });
    };


    function bindEvents() {
        if (boundEvents) return;

        //prepare live bindings if not already done.
        $('.' + ph)
            .live('click',itemIn)
            .live('focusin',itemIn)
            .live('focusout',itemOut);
        bound = true;

        boundEvents = true;
    };

    function itemIn() {
        var input = $(this)
            ,label = $(input.data(phl));

        label.css('display', 'none');
    };

    function itemOut() {
        var that = this;

        //use timeout to let other validators/formatters directly bound to blur/focusout work first
        setTimeout(function(){
            var input = $(that);
            $(input.data(phl))
                .css('top', input.position().top + 'px')
                .css('left', input.position().left + 'px')
                .css('display', !!input.val() ? 'none' : 'block');
        }, 200);
    };

}(jQuery));

and here’s the generated code on a field:

<input name="Number" title="Enter Gift Card 1 Number" class="CO_PM_GiftCardNumField PLACEHOLDER-INPUT" id="frmGCD_1_Number" style="width: 144px;" onkeypress="return checkEnter(event,'frmGCD_1')" type="text" maxLength="19" placeholder="Enter gift card" />
<label class="placeholder placeholder-for-input PLACEHOLDER-LABEL" id="frmGCD_1_Number_placeholder" style="position: absolute; display: block; top: 18px; left: 14px;" for="frmGCD_1_Number" >
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:01:32+00:00Added an answer on May 24, 2026 at 11:01 am

    ok – I fixed it.

    Since I was setting the focus with another script, I needed to add focus (not focusin) into the plugin:

    .live('click',itemIn)
    .live('focusin',itemIn)
    .live('focus',itemIn)
    .live('focusout',itemOut);
    bound = true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have the below jQuery which uses the colorbox plugin http://colorpowered.com/colorbox/ $(document).ready(function(){ $(.altbgcolor).hover(function()
I have this Javascript below using jQuery that shows a div on a page
I have a statement using jQuery as below: jQuery(#page>ul>li:last>a).click(); How can I do that
I am using the below jquery code to call a ajax function in my
i am adding collection of radio buttons to my page using jQuery below $(document).ready(function()
I am using jquery datepicker, please see below code $(document).ready(function() { $(.txtDate).datepicker({ showOn: 'button',
I am using JQuery and I have got below JQuery Code sample. JQuery Code:
I am using JQuery. I have below JQuery Code which working fine in Firefox,
I have some jQuery in my app that removes/shows a placeholder depending on the
How to generate page numbers like the below using javascript/jquery? If the 5 th

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.