1

Friends how can i do the following? I am having three input fields

 <input name="firstname" type="text" class="text" id="firstname" value="" />
 <input name="lastname" type="text" class="text" id="lastname" value="" />
 <input name="displayname" type="text" class="text" id="displayname" value="" />

I want to do this. After the user enters the first name and lastnaeme and then he clicks on the display name field, it should be populated with a value as "firstname lastname", How can I do it javascript?

0

2 Answers 2

2

Add onclick or keypress events on the displayname element with function -

function populateDisplayName(){
    var firstName = document.getElementById('firstname');
    var lastName = document.getElementById('lastname');
    document.getElementById('displayname').value = firstName.value + ' ' + lastName.value;
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

1
var fullName = document.getElementById('displayname');
var firstName = document.getElementById('firstName');
var lastName = document.getElementById('lastName');
fullName.value = firstName.value + ' ' + lastName.value;

1 Comment

need to put the above code in a function and attached that function to onclick event of displayname input text box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.