So this is a deceptively difficult question because you are trying to update PART OF the text inside a textarea. There is no structure inside of a textarea, it is just "text". So what what you're asking is similar to having a bucket of ping pong balls, writing a name on one of them, mixing up the bucket, then telling someone that they have to continuously replace the named ball with a new one.
While there are ways of doing this, it raises a bunch of questions that I doubt you want to answer - what if they delete the first line? What if they change their name directly in the textarea? What if they write their name multiple times in the text area? What if their browser uses a language that writes top-down?
Unless you are prepared to answer these queries (and if you are you should probably start a more specific question) I recommend changing your approach. For example, I doubt you want them to have the ability to change the first line. So instead you can structure your form as follows:
<form>
<label>First Name: <input class="first-name" /></label>
<label>Last Name: <input class="last-name" /></label>
<aside class="salutations">Dear <span class="first-name-salutation"></span></aside>
<textarea class="message"></textarea>
</form>
Notice I moved your inputs into the labels (this is the technically correct way of doing it without using ids), removed the <br>
tags (you should use css for this) and moved the salutation into non user-editable text. Then your javascript can be simply
$(function(){
$('input.first-name').on('input', function(){
$('.first-name-salutation').text($(this).text());
});
})
And then prepend "Dear "+firstName
line to the message on the server.
As an aside I'll point out that it's not 2009 anymore and there's little reason to use jquery directly for things like this when there are excellent frameworks that don't force you to do so many things manually like Knockout and Angular.