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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:15:06+00:00 2026-06-01T12:15:06+00:00

Is it possible to add a callback on setAttribute on Prototype ? For this

  • 0

Is it possible to add a callback on setAttribute on Prototype ?

For this example, I would like to show an alert("done !") when setAttribute() is finished.

img.setAttribute("src", "http://blabla.com/c.jpg");
  • 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-01T12:15:08+00:00Added an answer on June 1, 2026 at 12:15 pm

    You could, but I wouldn’t recommend it.

    (function() {
    
        var elementSetAttribute = Element.prototype.setAttribute;
    
        Element.prototype.setAttribute = function() {
            whateverFunctionYouWant.call(this);
    
            return elementSetAttribute.apply(this, arguments);
        }
    
    })();
    

    jsFiddle.

    This will call whateverFunctionYouWant() when you do something such as document.links[0].setAttribute('href', '/').

    You seem to want this to call a callback when you change the src attribute of img elements and the new resource has loaded…

    (function() {
    
        var HTMLImageElementSetAttribute = HTMLImageElement.prototype.setAttribute;
    
        HTMLImageElement.prototype.setAttribute = function(attribute, value) {
            var image;
    
            if (attribute == 'src') {
                image = new Image;
                image.addEventListener('load', loaded, false);
                image.src = value;
            }
    
            return HTMLImageElementSetAttribute.apply(this, arguments);
        }
    
    })();
    

    jsFiddle.

    You could overload setAttribute() here with a third parameter, which would be the callback.

    In my opinion and experience, I’d create a different function instead for doing this. Modifying prototypes is one thing, but overloading native DOM methods to do extra things sounds like inviting trouble, especially once a developer unfamiliar with what you’ve done looks at the code.

    It goes without saying that if you want the latter example to work < IE9, use the attachEvent() fallback for adding the event listener.

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

Sidebar

Related Questions

Is this possible to add variable to callback scope? What I want to achieve
Is it possible to add a callback to a single ActiveRecord instance? As a
Would it be possible to add a new operator to the String class that
Is it possible to add status text in jQuery progress bar? For example :
I would like to add onMouseOver and onMouseOut events to an editor instance within
With Paramiko, one can set up a SSH key callback like this: def someCallbackFunction(client,
Just wondering if it's possible to add a button like the Clear History, Clear
Is it possible to add messages to the built-in error console of Firefox from
Is it possible to add a list to a struct? public struct test {
Is it possible to add Dependent Breakpoints(Not a Conditional Bp) ( Breakpoint1 is enabled

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.