DEV Community

Discussion on: I have a problem to use "replaceChild" to active "if" and "else"

Collapse
 
ianowira profile image
Ian Owira

What is it that you're ultimately trying to achieve? I can see a few issues within your script which causes things thing to not work properly.

Collapse
 
andybullet profile image
AndyBullet

What I want it to do is to make the image appear if the first name written on the list is "Water" and I want to make the word "uff" appear if the first name written on the list is "Coffee".
I put the button "Change the first food" to change the fist name of the list.

Collapse
 
ianowira profile image
Ian Owira

Okay. So your main issues are that you're not using the correct properties for what you're trying to target.

let name = document.getElementById('food').value;

This line is failing because there is no value property for the getElementByID method. It's meant to be innerHTML if you want to retrieve the text value.

document.write(uff);

This here inside your else statement is also incorrect.

  1. 'uff' is a variable that has not been defined.
  2. document.write will clear the entire screen and print out whatever arguments you've placed inside there.

Here you can find a pen of more or less how it should look.