0
let fullName = prompt("Please enter your name :")
let surName = document.querySelector("#surName")
surName.innerHTML =`${surName.innerHTML}${surName}`

my piece of code is here and I'm getting an error like this. I can't import data into hmtl with inner.HTML and from prompt. What should I do ? Also index.html file

<h2 id="surName">Javascript Prompt</h2>
Tiddo
  • 6,331
  • 6
  • 52
  • 85
lisa
  • 15
  • 3
  • Hi, what exactly are you trying to achieve here? It's a little unclear. So your first 2 lines make sense, but are you trying to print the `fullName` in the `h2` tag? – ProblemChild Dec 05 '22 at 15:48
  • Hi, sorry I want to write the name entered with the prompt on the html page, but the fullName I entered with the prompt is not written on the html page. – lisa Dec 05 '22 at 15:58
  • I added the code below.. The reason it wasn't added is because you were passing `surName` to `#surName`, when you should be passing `fullName` to `#surName`.. :) – ProblemChild Dec 05 '22 at 16:02
  • You are most likely trying to read/set the value of an element before it exists on the page. – epascarello Dec 05 '22 at 17:24

1 Answers1

0

Try this:

let fullName = prompt("Please enter your name :");
let surName = document.querySelector("#surName") ;
surName.innerHTML = fullName; // because `fullName` now has the value you enter in the prompt
<h2 id="surName">

</h2>
ProblemChild
  • 556
  • 1
  • 8
  • 16