Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatanate 2 fields to form a label

Status
Not open for further replies.

planeboy747

Technical User
Joined
Jan 11, 2005
Messages
41
Location
US
I have 2 fields in my form, id= "lname1" and "fname1". The first field is last name and the other is first name.

I would like for the function to concatanate the 2 fields to form one string, like Last Name, First Name (Doe, John E.). The comma needs to be included when concatanated as well as the words need to be set to title case.

What I'm trying to do with these two fields is form a label in another part of the form. I'm thinking I may need to use a <script> tag and use document.write to call a function that will display this in the form (the concatanated lname1 and fname1 fields), but I'm not sure how the function should be written?

Any thoughts?

Thanks
J
 
Something like this:

Code:
<script language="javascript"><!--
function getLabel() {
    var f = document.forms['f'];
    var ln = f.elements['lname'].value;
    var fn = f.elements['fname'].value;
    var full = ln + ", " + fn;
    document.getElementById('targ').innerHTML = full;
}
--></script>

<form name="f">
Last: <input type="text" name="lname" onchange="getLabel();" /><br />
First: <input type="text" name="fname" onchange="getLabel();" /><br />
<br /><br />
<label id="targ" for="another_field"></label>
<input name="another_field" type="text" />
</form>

*cLFlaVA
----------------------------
[tt]tastes great, less filling.[/tt]
 
this is awesome thanks, however I've got one issue. My lname1 and fname1 fields are created by another script, because those fields are based on another element in the form...i tried to plug in the onchange item, but it didnt work, any ideas?

here's the script i have to create the lname and fname fields
function insertRows(isTable){

index = isTable.rows.length;
nextRow = isTable.insertRow(index);
isText1 = nextRow.insertCell(0);
isText2 = nextRow.insertCell(1);
index++;
index = index.toString();
nameStr1 = "Lname"+index;
nameStr2 = "Fname"+index;
txtStr1 = "Last Name "+index+":<br><input name="+nameStr1+" type='text' size='18' maxlength='18'>";
txtStr2 = "First Name "+index+":<br><input name="+nameStr2+" type='text' size='18' maxlength='22'>";
isText1.innerHTML = txtStr1;
isText2.innerHTML = txtStr2;
isText1.width ='25%'
isText2.width = '75%'
}

function adjustRows(isVal,isTable){

currRows = isTable.rows.length;
newRows = isVal;
if (currRows > 0){for (i=0; i<currRows-0; i++){isTable.deleteRow()}}
for (i=0; i<newRows; i++){insertRows(isTable)}
}
 
hey, got it to work! thanks for help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top