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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:39:14+00:00 2026-05-26T20:39:14+00:00

I have code that suppose to find the first child (div) of a div

  • 0

I have code that suppose to find the first child (div) of a div and than change its content, here is my code:

var headDiv = document.getElementById('sliderContainer');
var childDiv = headDiv.childNodes[0];
childDiv.innerHTML = 'Working';

that seems to not working, what’s the problem here?

live : http://jsbin.com/aqozef/edit#javascript,html,live

the full code :

<!doctype html>
<html>
<head>
    <script type="javascript">
        function scrollit(){
            var headDiv = document.getElementById('sliderContainer');
            var childDiv = headDiv.childNodes[0];
            childDiv.innerHTML = 'Working';
        }
    </script>
    <style type="text/css">
        #sliderContainer{width:100px;height:100px;overflow:hidden;}
        .sliderContent{width:100px;height:100px;float:left;}
    </style>
</head>

<body>
    <div id="sliderContainer">
        <div class="sliderContent" style="background-color:blue;">s</div>
        <div class="sliderContent" style="background-color:yellow;">a</div>
    </div><br/><br/>
    <button onclick="scrollit();">scroll it</button>
</body>
</html>
  • 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-26T20:39:15+00:00Added an answer on May 26, 2026 at 8:39 pm

    It is likely that the first child node is a text node and not an element node. This is the case, for example, when your HTML looks like this:

    <div>
        <div> I'm the first element child</div>
    </div>
    

    The line break after the first div and the spaces before the second one are one text node.

    To get the element nodes, use children [MDN]:

    headDiv.children[0].innerHTML = "Working";
    

    Note: IE6 – IE8 include comment nodes in children as well. As long as you have no comments there, it is fine.


    Alternatively you can traverse the child nodes, looking for the first element node:

    var child = element.firstChild;
    while(child && child.nodeType !== 1) child = child.nextSibling;
    

    Reference: Node.firstChild, Node.nodeType, Node.nextSibling.


    If you want to change the value of a text node, you have to change the nodeValue [MDN] property:

    node.nodeValue = "Working";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first some code: I have many elements like that: <section class=item> <div class=caption> </div>
The following code is supposed to find electors that do not have a postal
I have this code that suppose to place the marker by the latlng public
Suppose I have code that maintains a parent/children structure. In such a structure I
suppose that,we have following code auto_ptr<T> source() { return auto_ptr<T>( new T(1) ); }
Suppose I have a line of code that starts like the following: Func1(Func2(Func3 Is
Suppose we have the xhr 'POST' request that returns 'redirect' status code. In that
I have the following code that is supposed to find an element inside of
I found this jQuery code that allows to center a div that doesn't have
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent

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.