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

  • SEARCH
  • Home
  • 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 7817749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:20:30+00:00 2026-06-02T06:20:30+00:00

I’m using the following placeholder plugin (function($){ var ph = PLACEHOLDER-INPUT; var phl =

  • 0

I’m using the following placeholder plugin

(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;

    //bind to resize to fix placeholders when the page resizes (fields are hidden/displayed, which can change positioning).
    $(window).resize(checkResize);


    $.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
            itemFocus.call(this);
            itemBlur.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).unbind('change',itemChange);
        });
    };

    function bindEvents() {
        if (boundEvents) return;

        //prepare live bindings if not already done.
        $("form").live('reset', function(){
            $(this).find('.' + ph).each(itemBlur);
        });
        $('.' + ph)
            .live('keydown',itemFocus)
            .live('mousedown',itemFocus)
            .live('mouseup',itemFocus)
            .live('mouseclick',itemFocus)
            .live('focus',itemFocus)
            .live('focusin',itemFocus)
            .live('blur',itemBlur)
            .live('focusout',itemBlur)
            .live('change',itemChange);
            ;
        $('.' + phl)
            .live('click', function() {  $($(this).data(ph)).focus(); })
            .live('mouseup', function() {  $($(this).data(ph)).focus(); });
        bound = true;

        boundEvents = true;
    };

    function itemChange() {
        var input = $(this);
        if (!!input.val()) {
            $(input.data(phl)).hide();
            return;
        }
        if (input.data(ph+'FOCUSED') != 1) {
            showPHL(input);
        }
    }

    function itemFocus() {
        $($(this).data(ph+'FOCUSED',1).data(phl)).hide();
    };

    function itemBlur() {
        var that = this;
        showPHL($(this).removeData(ph+'FOCUSED'));

        //use timeout to let other validators/formatters directly bound to blur/focusout work
        setTimeout(function(){
            var input = $(that);

            //if the item wasn't refocused, test the item
            if (input.data(ph+'FOCUSED') != 1) {
                showPHL(input);
            }
        }, 200);
    };

    function showPHL(input, forced) {
        var label = $(input.data(phl));

        //if not already shown, and needs to be, show it.
        if ((forced || label.css('display') == 'none') && !input.val())
            label
                .text(input.attr('placeholder'))
                .css('top', input.position().top + 'px')
                .css('left', input.position().left + 'px')
                .css('display', 'block');

        //console.dir({ 'input': { 'id':input.attr('id'), 'pos': input.position() }});
    }

    var cr;
    function checkResize() {
        if (cr) window.clearTimeout(cr);
        cr = window.setTimeout(checkResize2, 50);
    }
    function checkResize2() {
        $('.' + ph).each(function(){
            var input = $(this);
            var focused = $(this).data(ph+'FOCUSED');
            if (!focused) showPHL(input, true);
        });
    }

}(jQuery));  

It applies the placeholder attribute to form fields in browsers that do not natively support the placeholder attribute (ex. IE9). It works for statically loaded text fields, however for text fields that are loaded via ajax, the placeholder does not appear.

Is it possible to achieve this ‘watermark’ effect on text fields that are loaded via ajax?

  • 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-06-02T06:20:32+00:00Added an answer on June 2, 2026 at 6:20 am

    What happens if you trigger the window resize function after adding in new inputs?

    $(window).trigger('resize')

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but

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.