when i execute this function by clicking “SAY HI!!” all the HTML code are getting erased or rewritten . Here is the code. Why is this happening ? Also refreshing is not resetting the page.
"Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open, which will clear the document. "
@AymanGhaith is correct. What and where do you want to change. Your current code is removing everything.
…
You are writing to the document, so anything inside it will be replaced. You should also be passing a string containing the HTML you want to write to the document.
function welcome(nm) {
alert(nm);
var name = prompt("what is your name");
document.write(`<h1>Hello ${name}</h1>`);
}
I would not suggest using document.write() for DOM manipulation.