View Full Version : Join two input values into one value in html or javascript codes?

05-16-2009, 05:40 PM
hi, can somebody answer my question? i want an html code or javascript equivalent for this particular case.
i want to put two input values into one.in other words, join them together..example:

<input name="name" value="William"/>
<input name="surname" value="Shakespeare"/>

and then the next input would be the the combination of the first two input


input name="completename" value="(name + surname)"

to yield and input value of value="William Shakespeare"

thank you so much.any reply would be a great help.

Philip M
05-16-2009, 06:00 PM
Try this:-

<form name = "myform">
<input name="firstname" value="William" onblur = "combine()"><br>
<input name="surname" value="Shakespeare" onblur="combine()"><br>
<input name="completename" readonly=readonly<br>

<script type = "text/javascript">
function combine() {
var fn = document.myform.firstname.value;
var sn = document.myform.surname.value;
var cn = fn + " " + sn;
document.myform.completename.value = cn;

NB You should avoid giving an HTML element the name = name! Make it firstname.

He thought he saw an Elephant,
That practised on a fife:
He looked again, and found it was
A letter from his wife.
"At length I realise," he said,
"The bitterness of Life!"
- Lewis Carroll

05-16-2009, 06:03 PM
See if this will work for you:

<title>Input Joining</title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?t=166484

function FillIn() {
document.getElementById('fullname').value =
document.getElementById('fname').value + ' ' + document.getElementById('surname').value;
<body onLoad="FillIn()">
<input id="fname" name="fname" value="William" onblur="FillIn()" />
<input id="surname" name="surname" value="Shakespeare" onblur="FillIn()" />
<input id="fullname" name="fullname" value="" />


It's not fair that 'PhillipM' types faster than me ... my Native American name is "TypesToSlow". :D

05-17-2009, 02:40 PM
hey both of you are great,,i use your codes and it helps me a lot..both of your codes work!!!again thank you philip m and jmrker