I have the follwing JavaScript.
With addition to dystroy answer, you could replace document.write
with:
document.body.innerHTML += '<h1>Javascript demo</h1>
use document.write()
at the end of all JS statements. The script
element is never executed after it.
You can write to the document with
document.write("<h1> Just a javascript demo</h1>");
only once and it applies to the whole document. If you want to put you will have to add a class/id to it and then put text in that class/id.
document.write can only be used during the initial loading of the document.
If you want to insert your H1 when the function is called, you may replace
document.write("<h1> Just a javascript demo</h1>");
with
var h1 = document.createElement('h1');
h1.innerHTML = " Just a javascript demo";
document.body.appendChild(h1);
You're destroying the DOM with your document.write call. In some browsers, this also destroys global variables.
Instead use:
var element = document.createElement('h1');
element.appendChild(document.createTextNode('text'));
document.body.appendChild(element);
Try this:
var x=document.getElementById(txt1).value;
alert(x);