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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:03:49+00:00 2026-05-23T10:03:49+00:00

I am teaching myself javascript and don’t understand the results I am getting from

  • 0

I am teaching myself javascript and don’t understand the results I am getting from some example code. I have pruned the code for brevity.

The html:

<body>
        <h1>firstChild and lastChild Property Lab</h1>
        <hr />
        <form>
            <label>Enter some text to add or replace in the OL element:</label>
            <br />
            <input type="text" name="input" size="50" />
            <br />
            <input type="button" value="Insert at Top"
                    onclick="prepend(this.form)" />
            <input type="button" value="Append to Bottom"
                    onclick="append(this.form)" />
            <br />
            <input type="button" value="Replace First Item"
                    onclick="replaceFirst(this.form)" />
            <input type="button" value="Replace Last Item"
                    onclick="replaceLast(this.form)" />
        </form>
        <ol id="myList">
            <li>Initial Item 1</li>
            <li>Initial Item 2</li>
            <li>Initial Item 3</li>
            <li>Initial Item 4</li>
        </ol>
    </body>

The javascript:

// helper function for prepend() and append()
function makeNewLI(txt)
{
    var newItem = document.createElement("LI");
    newItem.innerHTML = txt;
    return newItem;
}

function replaceFirst(form)
{
    var newItem = makeNewLI(form.input.value)
    var firstLI = document.getElementById('myList').firstChild.value;
    document.getElementById('myList').replaceChild(newItem, firstLI);
}

I thought if I entered some text in the text box and clicked the Replace First Item button that the first item in the list would be replaced. However, the first time I do that it is prepended to the top of the list and the list now has 5 items. Then if I change the text in the text box and click the Replace First Item button again the first item in the list is changed.

I don’t understand why this is happening. I expected the replacement would happen whenever I clicked the button. I am working with Firefox 4 on Ubuntu 10.04. Also, if I try to replace the last item in the list result is the same as I outlined above.

Thanks, Jim

  • 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-23T10:03:50+00:00Added an answer on May 23, 2026 at 10:03 am

    There are 2 things wrong there. Firstly, you are selecting the value instead of the element with var firstLI = document.getElementById('myList').firstChild.value; so use var firstLI = document.getElementById('myList').firstChild; instead.

    Secondly, believe it or not your firstChild is a TextNode with empty text with your current markup. Remove the empty space between <ol> and the first <li> so that its <ol><li>...., like here:

    http://jsfiddle.net/niklasvh/5anyq/

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

Sidebar

Related Questions

I am teaching myself some Javascript and don't understand why Chrome console tells me
not sure why my code wont work, im teaching myself javascript i know php
This is my first week teaching myself javascript and i have run into a
I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project. The
I am teaching myself some Python and I have come across a problem which
Teaching myself some c# and my problem is that I have a class which
Still teaching myself python so please don't hate me if my code is terrible...
I am teaching myself C# (I don't know much yet). In this simple example:
I am self teaching myself some python OOP, and I have created a simple
I'm teaching myself some basic scraping and I've found that sometimes the URL's that

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.