the a tag is not appearing

in the javascript code before i added text.innerHTML = "Hello world and welcome"; the username.innerHTML = name was working normally and now it does not. it just disappears unless i remove text.innerHTML = "Hello world and welcome";
3 Replies
MarkBoots
MarkBoots3mo ago
well, this is your html
<p id="text">
Click the text above!
<a id="username"></a>
</p>
<p id="text">
Click the text above!
<a id="username"></a>
</p>
as soon as you set the innerhtml of text to Hello world and welcome the html becomes
<p id="text">Hello world and welcome</p>
<p id="text">Hello world and welcome</p>
So, the <a> does not exist anymore, because you just changed the entire inner html put the text inside a span and it's easier to manage
<p>
<span id="text">Click the text above!</span>
<a id="username"></a>
</p>
<p>
<span id="text">Click the text above!</span>
<a id="username"></a>
</p>
const text = document.querySelector("#text");
const username = document.querySelector("#username");

//instead of innerHTML, it's safer to use innerText
text.innerText = "Hello world and welcome"
username.innerText = name;
const text = document.querySelector("#text");
const username = document.querySelector("#username");

//instead of innerHTML, it's safer to use innerText
text.innerText = "Hello world and welcome"
username.innerText = name;
Yido
Yido3mo ago
its works! thank you :)