...

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



dadynidhen
05-16-2009, 04: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

example:

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, 05: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>
</form>

<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;
}
</script>

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

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


<html>
<head>
<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;
}
</script>
</head>
<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="" />
</body>
</html>

:)

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

dadynidhen
05-17-2009, 01: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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum