Mr Petroleum Mr Petroleum - 27 days ago 19
HTML Question

Open new window on submit

i have a question in html. I already wrote my code but now I want to open a new window after someone clicked the submit button and in this new window the text he or she entered shall be displayed.

<html>
<body>
<font face="Arial,Helvetica">Hello!
</body>
<p><label for="vname">Tell me something:
<input type="text" id="vname" name="vname">
</font>
<body>
</p>
</label>
<input type="submit" value="Submit" onclick="myAlert()">
</html>

Answer

First of all, you should use a <form>. Otherwise, the <input type="submit"> doesn't really do anything. Once you have that, use window.open().

<form onsubmit="return false;">
<label for="vname">Tell me something:
<input type="text" id="vname" name="vname">

<input type="submit" value="Submit" onclick="myAlert()">
</form>

And the JavaScript:

<script>
function myAlert() {
var results= window.open();
var vname = document.getElementById("vname").value;
results.document.write(vname);
}
</script>

Basically, the JS gets the value of the input, opens a new page, and then uses document.write to display the results.

Comments