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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:58:12+00:00 2026-06-17T19:58:12+00:00

A project I’m working on has to manipulate DOM elements using JavaScript. When a

  • 0

A project I’m working on has to manipulate DOM elements using JavaScript. When a particular event fires, I’m looping through the DOM and basically creating an index of all of the nodes in order to manipulate them later. There is more to the reason why I’m creating this index, but for simplicity sake I’m dumbing it down here.

However the problem is that when we go to manipulate the element in the DOM, it seemingly manipulates a separate object than the actual object in the DOM. Here is about as basic of an example as I could come up with:

<div id="container">
    <div>Here is some text</div>
    <div>And here is some more text</div>
</div>

<script>
    var container = document.getElementById('container');
    container.firstChild.innerHTML = 'Foobar';
</script>

Fiddle: http://jsfiddle.net/4Nngh/

But the first div is never changed to “Foobar”.
I’m guessing when I get the “container” element I am actually getting a separate object instead of a reference to the DOM element.

Is it possible to simply reference the element displayed in the DOM and manipulate it directly, or do I have to use some type of replacement method? Other events may change the contents of that DOM element while the script runs, so I’m trying to avoid having to re-create this index every single time the event fires, which is why I’m looking for a “reference” to the element somehow.

Thanks!

  • 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-17T19:58:13+00:00Added an answer on June 17, 2026 at 7:58 pm

    The firstChild is actually the text node containing the newline-tab combo.

    To get the first element child, use children[0]. The children[] array only includes element nodes.

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

Sidebar

Related Questions

The project I am currently working on has, in the checkout, an option to
The project I'm working on has an Oracle backend, and we use SQL Navigator
The project my team has been working on has reached a point where we
The project that I am working on (Node.js) implies lots of operations with the
The project currently has a UIviewController called Dashboard that acts as the main view
This project is in Obj-C for iphone. I'm using the double float version of
Project I'm working on uses jQuery. I have a series of Ajax calls being
Project file here if you want to download: http://files.me.com/knyck2/918odc So I am working on
Project was working on PC number 1. Copied project to external drive and trying
Our project is using many static libraries to build the application. How can we

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.