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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:13:07+00:00 2026-06-10T06:13:07+00:00

I’m looking for a way to add a javascript object to the DOM without

  • 0

I’m looking for a way to add a javascript object to the DOM without redefining the object.

I have created a function that puts all object methods into a new object, redefines the object as a new DOM element, and puts all the objects back onto it but this is not exactly what I want. I need the object to be updated without using any return statements.

I have an example here: http://jsfiddle.net/y56wS/

function addToDOM(obj) {
    temp = {};
    for (p in obj) {
        temp[p] = obj[p];
    }
    obj = document.createElement('prop');
    document.body.appendChild(obj);
    for (p in temp) {
        obj[p] = temp[p];
    }
    return obj;
}

var obj = {var1:123};
obj = addToDOM(obj);

The resulting code template should be something like this:

function addToDOM(obj) {
    //Code for method
    //No return statement
}

var obj = {var1:123};
//No obj=
addToDOM(obj);

To accomplish this, there really has to be no obj = within the addToDOM function so that it is never redefined and there is no need for a return statement. Is there a way to simply extend the javascript object onto the DOM?

  • 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-10T06:13:08+00:00Added an answer on June 10, 2026 at 6:13 am

    No, JavaScript objects can’t be added to the document just like that – the appendChild() method is expecting objects of certain type, usually created with document.createElement() method.

    So you must create new DOM element, or clone existing one, every time.

    Assuming you want to have sort of “template” element I suggest using global variable to hold that element, create it once then clone it every time and put the new attributes.

    Code for that would be:

    var _globalDOMobj = 0;
    function addToDOM(obj) {
        temp = {};
        for (p in obj) {
            temp[p] = obj[p];
        }
        if (!_globalDOMobj) {
            _globalDOMobj = document.createElement('prop');
            _globalDOMobj.className = "myprop";
        }
        obj = _globalDOMobj.cloneNode(true);
        obj.innerHTML = "I am dynamic element";
        document.body.appendChild(obj);
        for (p in temp) {
            obj.setAttribute(p, temp[p]);
        }
        return obj;
    }
    
    addToDOM({"var1": "123", "style": "color: red;"});
    addToDOM({"var2": "789", "style": "color: blue;"});
    

    As you see, you can pass the object directly to the function no need to assign it to variable first.

    Also, to have the object properties applied in the DOM element you have to use setAttribute() otherwise those properties won’t be part of the DOM.

    In the above code, the template has the same class for all instances, so you can apply some CSS to affect them all e.g.

    .myprop { font-weight: bold; }​
    

    ​Live test case.

    • 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’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.