How do I return a variable value (test) in multiple places in my HTML <p>
tag.
The variable displays correctly in the first instance but is blank in the second. What am I doing wrong?
var name = document.querySelector("#NameInput").value;
function fillData() {
document.querySelector("#nameOutput").innerHTML = name;
}
<p>I understand your name is <span><output id="name"></output></span>.</p>
<p>It's nice to meet you `your text`<span><output id="name"></output></span>.</p>
2
Answers
Your IDs should be unique. Also, the id "nameOutput" does not exist in your HTML code.
Here’s a quick solution: