0

I am trying to open a new window with values received from textfield with id "name";

<form name="myForm">
<input type="text" id="name">
</form>

<button onclick="openWindow();">Open</button>

And this is the javascript:

function openWindow() {
newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");  

newWindow.document.write("<h1>"+document.myForm.name.value+"</h1>");
}

It does it ok, but why does it empty the form values?
And how can i center this new window? Maybe height=window.height()/2 ?

4
  • Create a popup? Or a window? Two different things. Why not use alert() Commented Nov 11, 2014 at 20:35
  • @GeekByDesign window sorry, i need this new window to be interactive later on, to change values etc... Commented Nov 11, 2014 at 20:36
  • Because you aren't declaring myForm as the actual element. Or at all. Commented Nov 11, 2014 at 20:37
  • @SpencerMay where should i declare it, outside the openWIndow method, like var someText[] = document.myForm ? Commented Nov 11, 2014 at 20:43

2 Answers 2

1

disable the onclick event, it's submitting the form. Add this to the form tag, then at the bottom of the function return false. onsubmit="return openWindow();">

Sign up to request clarification or add additional context in comments.

1 Comment

as for centering the window put` var left = (screen.width/2)-(w/2); var top = (screen.height/2)-(h/2);` in your function and put`top='+top+', left='+left) in your window open
0

It works fine on my end, tried your code. Dont forget to write something inside the input box, before you click the button. if it doesn't work

Try this instead:

newWindow.document.write("<h1>"+document.getElementById('name').value+"</h1>");

1 Comment

pff neither works, still empties the form when the new window pops up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.