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 8461803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:58:42+00:00 2026-06-10T13:58:42+00:00

How to make a jquery function work for elements added after its first run

  • 0

How to make a jquery function work for elements added after its first run which does not apply to dynamically added elements.

Code:

jQuery(function() {
   jQuery.support.placeholder = false;
   test = document.createElement('input');
   if('placeholder' in test) jQuery.support.placeholder = true;
});

$(function() {
   var active = document.activeElement;
   if(!$.support.placeholder) { 
      $(':text').focus(function () {
         if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
            $(this).val('');
            if($(this).hasClass("dcphc")===true){$(this).removeClass('dcph');}
            else if($(this).hasClass("dcphmgc")===true){$(this).removeClass('dcphmg');}
            else if($(this).hasClass("dcphlgc")===true){$(this).removeClass('dcphlg');}
         }
      }).blur(function () {
         if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val($(this).attr('placeholder'));
            if($(this).hasClass("dcphc")===true){$(this).addClass('dcph');}
            else if($(this).hasClass("dcphmgc")===true){$(this).addClass('dcphmg');}
            else if($(this).hasClass("dcphlgc")===true){$(this).addClass('dcphlg');}
         }
      });
      }
      else{
        $(':text').blur(function () {
         if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val("");
         }
      }); 
      }
      $(':text').blur();
      $(active).focus();
});

function ref_dcph(w){
      var active = document.activeElement;
      if(!$.support.placeholder) { 
      $(w).focus(function () {
         if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
            $(this).val('');
            if($(this).hasClass("dcphc")===true){$(this).removeClass('dcph');}
            else if($(this).hasClass("dcphmgc")===true){$(this).removeClass('dcphmg');}
            else if($(this).hasClass("dcphlgc")===true){$(this).removeClass('dcphlg');}
         }
      }).blur(function () {
         if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val($(this).attr('placeholder'));
            if($(this).hasClass("dcphc")===true){$(this).addClass('dcph');}
            else if($(this).hasClass("dcphmgc")===true){$(this).addClass('dcphmg');}
            else if($(this).hasClass("dcphlgc")===true){$(this).addClass('dcphlg');}
         }
      });
      }
      else{
        $(w).blur(function () {
         if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
            $(this).val("");
         }
      }); 
      }
      $(w).blur();
      $(active).focus();
}

First function does the work, the second one ref_dcph (reference dom control place holder)
is a copy of the first one but I use it every time I add new elements that have a particular identifier other than being input or textarea so that I don’t have to go through unbind on items to which the function is already attached.

I would need a twist on the first one for it to be run also on new dynamycally elements, I don’t understand how to use “on”.

—
Also, what would happen to elements that get removed from the dom and added again (with the same id – being the same element also) – I noticed with the click handler if you want to do a $(elem).click(function(){}); you have to go through unbind – else the click is doubled.

I wonder how to make the first twist and also check if a previous focus() or blur() was attached to the same element that is being detected as dynamically generated and if it is the case then don’t attach again focus() and blur(). Cause I have elements on the page that get removed from the dom and then are added again which involve an input or a textarea with a placeholder.

  • 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-10T13:58:44+00:00Added an answer on June 10, 2026 at 1:58 pm
              $("body").on("blur", ":text", function(event){});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After loading a PHP template (using jQuery's load function), this simple script won't make
I am trying to make a button with click function to run my jquery:
My first foray into writing jQuery functions. I have this function, but I'm not
I'm using the jQuery .scroll() function to make a certain element fade to 0.2
I'm trying to make simple script using jquery $post function to pass data to
I can't make the delay function of jQuery works with append function. What is
I want to make a generic function in jquery for select all functionality. I
I make a call to a php file using jquery's ajax function. It goes
jQuery.widget(ui.test, { _init: function(){ alert(this.element.innerHTML); } }); How do I make it so when
I need to make some part of text bold while displaying in jquery: $(':checked').each(function()

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.