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 :)

Recommended Answers

All 3 Replies

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <script type="text/javascript">
	<!--
	function init(){
		getName();
		//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"..
		}else{
			alert("input your name. plz");
			getName();
		}
		
	}
	//-->
  </script>

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

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 :) ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.