Using JavaScript onblur to populate text fields

Hi,
   What i am trying to do is populate a text field with the data that i enter in the previous field.
e.g when i enter a persons name in the first field i want to append, say "@hotmail.com", to it and add the whole value automaticaly to the next text field.

I understand you can do this by using Javascript onBlur but i'm not sure how.

Any help would be much appreciated

Thanks
mickmurphAsked:
Who is Participating?
 
alexprestonCommented:
Not sure I'd use onkeypress. I'd consider it overkill to change the email field for every keypress. But it is a valid solution!
0
 
alexprestonCommented:
something like...

<input type="text" id="txtName" onblur="Append();">
<input type="text" id="txtEmail">

<script>
function Append()
{
txtEmail.text = txtName.text + '@hotmail.com';
}
</script>
0
 
mr_asphaltCommented:
that should be:
txtEmail.value = txtName.value + '@hotmail.com';
0
 
alexprestonCommented:
you're quite right, a slap on the wrists for me :)
0
 
riyasjefCommented:
this is a better method using "onkeypress"

<form name="frm">
      <input type="text" id="txtName" onkeypress="Append(this.value);">
      <input type="text" id="txtEmail">
</form>      

<script>
function Append(str)
{
       document.frm.txtEmail.value=str+'@hotmail.com';

}
</script>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.