Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
I am trying to use .replaceChild to replace an ordered list item with the value of a prompt

In my program it lists three items in an ordered list which all have ids. Then when you click a button it prompts you to enter the number corresponding to which numbered item on the list you want to replace. Then another prompt asks what you want to replace the text on the screen with. I have if statements set up to check for which number is entered and using .replaceChild to replace text in the list with the text entered in the prompt. I assumed this would work similar to how using .innerHTML does in replacing text, but it is not working for me. Any help would be appreciated.

Here is my code.

function replaceItem() 
{ 
var listNum = prompt("Which item are you replaceing 1, 2 or 3?");
var newItem = prompt("What is the name of the new item?");

if (listNum === 1) {
var item_one = document.getElementbyId("item1");
item_one.replaceChild(newItem, item_one);
}

if (listNum === 2) {
var item_two = document.getElementbyId("item2");
item_two.replaceChild(newItem, item_two);
}

if (listNum === 3) {
var item_three = document.getElementbyId("item3");
item_three.replaceChild(newItem, item_three);
}

}

HTML.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Island Scenario</title>
</head>

<body>
<h3>You are being sent to an island by yourself to survive for 1 week.</h3> <br>
<h3>You are allowed to bring the clothes on your back but are also being given three items.</h3> <br>
<h3>Out of the three items that you are initially given, you are allowed to switch out one of the items for something of your choice.</h3> <br>
<h4>Here are your current items.</h4>
<ul>
<li id="item1">Water Bottle</li>
<li id="item2">Lighter</li>
<li id="item3">Backpack</li>
</ul>
<p>Click the button to replace an item.</p>
<button onclick="replaceItem();">Click Me</button>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In addition to the typo in your attempt, this isn't the way to use replaceChild. With replaceChild, you need to specify which HTML node element to replace, along with the new element to replace with. Here's an easier way. Just use the natural index of the elements and innerText to replace the one you want. I gave the UL a className to make this a better example.

In this line, document.querySelectorAll('.theList li') makes an iterable HTMLElement list and [listNum - 1] takes the input, subtracts one to get the correct LI element.

document.querySelectorAll('.theList li')[listNum - 1].innerText = newItem;

function replaceItem() {
  var listNum = prompt("Which item are you replaceing 1, 2 or 3?");
  var newItem = prompt("What is the name of the new item?");
  document.querySelectorAll('.theList li')[listNum - 1].innerText = newItem;
  return
}
<h3>You are being sent to an island by yourself to survive for 1 week.</h3> <br>
<h3>You are allowed to bring the clothes on your back but are also being given three items.</h3> <br>
<h3>Out of the three items that you are initially given, you are allowed to switch out one of the items for something of your choice.</h3> <br>
<h4>Here are your current items.</h4>
<ul class='theList'>
  <li id="item1">Water Bottle</li>
  <li id="item2">Lighter</li>
  <li id="item3">Backpack</li>
</ul>
<p>Click the button to replace an item.</p>
<button onclick="replaceItem();">Click Me</button>

about 4 years ago · Juan Pablo Isaza Report

0

The replaceChild is not the best way to handle this scenario, since replacing the innerText or textContent of the Node would be more straightforward, but if you want to know why it's not working, that is because replaceChild wants two HTML nodes as params, newNode and oldNode, since you have text nodes inside the li elements, you need to get them with childNodes property, same goes for the newNode you need to create a textNode with document.createTextNode.

Other than this, I suggest you to make a function to handle that logic to follow DRY principles, since you are repeating code and the only thing that changes is the selector of the parent:

function replaceItem() {
  const listNum = prompt("Which item are you replaceing 1, 2 or 3?");
  const newItem = document.createTextNode(prompt("What is the name of the new item?"))
  replace(listNum, newItem)
}

function replace(n, newNode) {
  const parent = document.getElementById(`item${n}`);
  const oldNode = parent.childNodes[0]
  parent.replaceChild(newNode, oldNode);
}
<body>
  <h3>You are being sent to an island by yourself to survive for 1 week.</h3> <br>
  <h3>You are allowed to bring the clothes on your back but are also being given three items.</h3> <br>
  <h3>Out of the three items that you are initially given, you are allowed to switch out one of the items for something of your choice.</h3> <br>
  <h4>Here are your current items.</h4>
  <ul>
    <li id="item1">
      Water Bottle
    </li>
    <li id="item2">
      Lighter
    </li>
    <li id="item3">
      Backpack
    </li>
  </ul>
  <p>Click the button to replace an item.</p>
  <button onclick="replaceItem();">Click Me</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!