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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:19:29+00:00 2026-05-22T12:19:29+00:00

I would like to alter the functionality of $.prepend() (and probably $.append() ) for

  • 0

I would like to alter the functionality of $.prepend() (and probably $.append()) for the purpose of having an “on DOM change event”.

Can I do something as simple as:

$.prepend = function() { alert('Hello World'); };

Or do I need to use the $.extend() function or $.prototype.prepend or $.fn.prepend?

[I realise I’ll need to include the original source for the prepend() function in my new one otherwise jQuery will break!]

EDIT :: Final Solution

For those who are interested:

$.extend($, {
domChangeStack: [],
onDomChange: function(selector, fn, unbind, removeFromStack) {
    /* Needs to store: selector, function, unbind flag, removeFromStack flag */
    jQuery.domChangeStack.push([selector, fn, unbind, removeFromStack]);
},
domChangeEvent: function() {
    /* Ideally should only affect inserted HTML/altered DOM, but this doesn't */
    var stackItem, newStack = [];

    while (stackItem = jQuery.domChangeStack.pop()) {
        var selector = stackItem[0],
            fn = stackItem[1],
            unbind = stackItem[2],
            remove = stackItem[3];

        if (unbind) { $(selector).unbind(); }
        // Need to pass the jQuery object as fn is anonymous
        fn($(selector));
        if (!remove) { newStack.push(stackItem); }
    }

    jQuery.domChangeStack = newStack;

    // Show something happened!
    console.log("domChangeEvent: stack size = " + newStack.length);
}
});

$.fn.prepend = function() {

    var result = this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.insertBefore( elem, this.firstChild );
        }
    });

    // Need to actually alter DOM above before calling the DOMChange event
    $.domChangeEvent();

    return result;
};

And usage:

/* Run the given function on the elements found by the selector,
 *  don't run unbind() beforehand and don't pop this DOMChange
 *  event off the stack.
 */
$.onDomChange(".element_class", function(jObj) {
       jObj.do_something_awesome();
   }, false, false);
  • 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-22T12:19:30+00:00Added an answer on May 22, 2026 at 12:19 pm

    Which method you want to use depends on how much you need to change. Since .prepend is merely a method that resides in .fn you don’t have to mess with the prototype.

    I most cases its enough to rename the original method, create your own function that does what you want and end with a call to the original function, like this:

    var orgPrepend = $.fn.prepend;
    $.fn.prepend = function(){
        // Do what you want here
    
        // Call org prepend and return
        return orgPrepend.apply(this, arguments);
    }
    

    Note: .apply and .call are more or less identical. The only difference is that .apply passes arguments by reference while .call passes them by value, so I prefer to use .apply before .call where possible. See MDC for reference

    But if you look at the source of jQuery (see src/manipulation.js) you’ll see that this method is very small so you can just implement it directly.

    In the next example I will use .extend instead, but it’s not a must; you could just replace it like in the first example.

    $.extend($.fn, {
        prepend: function() {
            // Do what you want here
            console.log("prepend was called");
            // Execute domManip
            return this.domManip(arguments, true, function( elem ) {
                if ( this.nodeType === 1 ) {
                    this.insertBefore( elem, this.firstChild );
                }
            });
        }
    });
    

    You can override .domManip or any other method in the same way, like in the following example. You’ll probably see why I prefer to use .extend here.

    var _domManip = $.fn.domManip;
    $.extend($.fn, {
        domManip: function() {
            // Do what you want here
            console.log("domManip was called");
            _domManip.apply(this, arguments);
        },
        prepend: function() {
            // Do what you want here
            console.log("prepend was called");
            // Execute domManip
            return this.domManip(arguments, true, function( elem ) {
                if ( this.nodeType === 1 ) {
                    this.insertBefore( elem, this.firstChild );
                }
            });
        }
    });
    

    See test case on jsFiddle

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

Sidebar

Related Questions

I would like to post-process my app.config file and perform some token replacements after
I have a page where I would like it to remain static after refresh
I have a java.hprof.txt file (automatically generated after an OutOfMemoryError) which I would like
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,
I would like to filter an array of items by using the map() 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.