I making a form, nothing special, just for myself... so when user loads my form it askes him for his name ( with prompt 'var name=prompt("Your name please",""); ) then says Welcome 'name' (with alert box). THEN he can start filling in the form. What I want is for a user not to have to type his name two times but to pass the value of var name into the input type="text" name="First Name" ..

any suggestions would be appreciated :)

7 Years
Discussion Span
Last Post by phoenix_sdl
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <TITLE> New Document </TITLE>
  <script type="text/javascript">
	function init(){
		//and other scripts that execute onload event

	function getName(){
		var nameForm=document.frm;
		var name=prompt("your name plz","");//save inputted prompt value to var name..
		if(name != null && name!=""){
			alert("welcome ! "+ name);
			nameForm.fname.value=name;//and put it to fname. input type="text"..
			alert("input your name. plz");

 <BODY onload="init()">
	<form name="frm">
		<input type="text" name="fname"/>

very simple example...:)


The way asmira suggested is correct (even though there are many other ways to deal with). I just want to add some thing here if you are going to go that way.

1) You must not have multiple forms with the same name.
2) The check for name does not catch a string which contains only white space. You could use name=name.replace(/^\s+|\s+$/g) in order to get rid of leading/tailing white spaces and include in the condition as well.
3) You may not need to put it in init() if you don't want to; however, you will need to explicitly call the function somewhere in your page.


hey.. its working :) .. thx for advice taywin and asmira TNX a lot for code :) ..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.