Preserve line breaks in <textarea> while sending the HTML form data using JavaScript.
Hi friends,
Hope you are enjoying the time. Today I am posting a small trick about drafting mail to the given user using Javascript. Look at the following simple HTML page. The page includes user email address, subject and message body.
I have used <textarea> for getting the message body. You would be knowing that form input fields does not preserve the new line character (pressing "ENTER" button in the textarea). Here I am writing a simple code to preserve your line break or new line character while submitting the HTML form.
Look at the following code:
<html>
<head>
<title> CS-Sending Mail preserving Line breaks in the < textarea > </title>
<style>
textarea{white-space: pre-wrap;}
input{width: 250px; height: 25px; margin-top: 10px; padding: 2px; }
.main{width: 1010px; margin: 0 auto; padding: 10px; background: pink; line-height: 1.44}
.btn{width: 110px; padding: 7px; background: green; height: 35px; color: white; }
</style>
<script>
function sendMail(){
var str = "mailto:";
var emailAddr = document.getElementById("csEmail-1").value;
var subjectLineTxt = document.getElementById("csSubject").value;
var data = document.getElementById("CSaddr-1").value;
data = data.replace(/(\r\n|\n|\r)/gm,'%0D%0A');
/** mail format: mailto: cs_email@domain.com&subject=subjectText&body=BodyTextLines */
str += emailAddr+"?to=cs&subject="+encodeURI(subjectLineTxt)+"&body="+data;
document.getElementById("frm1").action = str;
}
</script>
</head>
<body>
<div class="main">
<form name="frm1" id="frm1" action="#" method="post" onsubmit="sendMail()">
<label>Email:</label> <input type="email"/ id="csEmail-1"><br/>
<label>Subject:</label> <input type="text"/ id="csSubject"><br/>
<label>Address: </label> <br/>
<textarea cols="60" rows="10" id="CSaddr-1"> </textarea> <br/>
<input type="submit" value="Submit" class="btn" >
</form>
</div>
</body></html>
Clicking on the submit button will open the client email program with auto filling the mail address, subject line and the body.
On Windows, the it would open the outlook window as follows:
Hi friends,
Hope you are enjoying the time. Today I am posting a small trick about drafting mail to the given user using Javascript. Look at the following simple HTML page. The page includes user email address, subject and message body.
I have used <textarea> for getting the message body. You would be knowing that form input fields does not preserve the new line character (pressing "ENTER" button in the textarea). Here I am writing a simple code to preserve your line break or new line character while submitting the HTML form.
Look at the following code:
<html>
<head>
<title> CS-Sending Mail preserving Line breaks in the < textarea > </title>
<style>
textarea{white-space: pre-wrap;}
input{width: 250px; height: 25px; margin-top: 10px; padding: 2px; }
.main{width: 1010px; margin: 0 auto; padding: 10px; background: pink; line-height: 1.44}
.btn{width: 110px; padding: 7px; background: green; height: 35px; color: white; }
</style>
<script>
function sendMail(){
var str = "mailto:";
var emailAddr = document.getElementById("csEmail-1").value;
var subjectLineTxt = document.getElementById("csSubject").value;
var data = document.getElementById("CSaddr-1").value;
data = data.replace(/(\r\n|\n|\r)/gm,'%0D%0A');
/** mail format: mailto: cs_email@domain.com&subject=subjectText&body=BodyTextLines */
str += emailAddr+"?to=cs&subject="+encodeURI(subjectLineTxt)+"&body="+data;
document.getElementById("frm1").action = str;
}
</script>
</head>
<body>
<div class="main">
<form name="frm1" id="frm1" action="#" method="post" onsubmit="sendMail()">
<label>Email:</label> <input type="email"/ id="csEmail-1"><br/>
<label>Subject:</label> <input type="text"/ id="csSubject"><br/>
<label>Address: </label> <br/>
<textarea cols="60" rows="10" id="CSaddr-1"> </textarea> <br/>
<input type="submit" value="Submit" class="btn" >
</form>
</div>
</body></html>
Clicking on the submit button will open the client email program with auto filling the mail address, subject line and the body.
On Windows, the it would open the outlook window as follows:
Comments
Post a Comment