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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:04:55+00:00 2026-06-01T09:04:55+00:00

I am writing a library that creates multiple elements inside a wrapper element, and

  • 0

I am writing a library that creates multiple elements inside a wrapper element, and stores all this and its functions inside a JavaScript object. I am trying to avoid IDs, as there might be multiple instances of this object on a page. I have a function to allow the user to change some of the elements, and I need help figuring out how to append an image.

Here is the function:

foo.prototype.rebrand = function(line1, line2, imgUrl){
    this.branding.childNodes[1].innerHTML = line1;
    this.branding.childNodes[2].innerHTML = line2;
    var brandImage = document.createElement('img');
    brandImage.onload = function(){
        this.branding.childNodes[0].appendChild(brandImage);
                    //this won't work
    }
    brandImage.src = imgUrl;
}

You would call foo.rebrand('hello', 'world', 'example.png')

Unfortunately, inside the .onload function, this will refer to the image element itself. So, how can I pass this.branding.childNodes[0] in to the image onload?

If I write the function like so:

            brandImage.onload = function(anything){
        this.branding.childNodes[0].appendChild(brandImage);
                    //this won't work
    }

then anything will just be a reference to the onload event.

Edit to add jsFiddle:
http://jsfiddle.net/KtJd6/

  • 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-01T09:04:56+00:00Added an answer on June 1, 2026 at 9:04 am

    You need to change the way you are referencing specific elements to retrieve elements, not childNodes. This will make it a lot more robust. And, when you do that, you won’t need the reference this in the onload handler either.

    foo.prototype.rebrand = function(line1, line2, imgUrl){
        var brandImage = document.createElement('img');
    
        // find child divs
        var divs = this.branding.getElementsByTagName("div");
        divs[0].innerHTML = line1;
        divs[1].innerHTML = line2;
    
        brandImage.onload = function(){
            divs[0].appendChild(brandImage);
        };
        brandImage.src = imgUrl;
    };
    

    Note, that I’m getting elements with getElementsByTagName() and NOT referring to direct childNode indexes. This makes it insensitive to where text nodes are and is a lot more robust way of referring to elements you want to target and modify.

    You can see it work here: http://jsfiddle.net/jfriend00/kkXCg/

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

Sidebar

Related Questions

So I'm writing a Javascript library that takes x,y coordinates and uses them to
I have been writing some code that creates a generic blog. Its features are
I'm writing a javascript / HTML5 canvas library to provide basic GUI elements for
I have a library that I created with some business logic that includes writing
Ok I am writing a library that will be shard between unix and windows.
I am writing a library that will provide a collection of public types to
I'm writing a little library that should be used by people who don't know
I am writing a Win32 DLL library that can be redistributed. I am using
While making some final tests of a class-library that I'm writing for Windows Mobile
Say that you're writing a library to display things on the screen, so you

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.