Skip to main content

TechCS: JavaScript Part-2: How to send HTML form data with preserving line breaks in the textarea

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:


Comments

Popular posts from this blog

How to Calculate HTML5 Video Height and Width dynamically to fit web page.

While developing the web pages with inline HTML5 videos we often need to adjust the video width or height according the available space on the web page.  Here is the solution to find that required height and width keeping the same aspect ratio of the video.  You may go ahead and set video CSS property to  width: 100% and height: auto but this will not give you any idea about how much height the video is going to take on the web page. Then the better approach would be setting the video height or width using Javascript. So, sometimes to adjust the width according to available height on the web page, it require to calculate the NEW WIDTH which you can set to your video as per available height. The same goes with Height as well. So here are the solutions to solve this problem. Here I am assuming the video is having 16:9 ratio, which would be a HD video with 1920px width and 1080px height. Now suppose I want my video to fit in the width of 1280px, then...

Most importment JavaScript frameworks & libraries for frontend development

Here is a list of top Most importment JavaScript frameworks & libraries for frontend development . 1. Jquery You can directly use jQuery library if your requirement is to deal with browser's DOM. jQuery has been a reliable, easy to use and handy library for frontend developers. You can trust on it's cross browser & legacy support. Along with simple jQuery.js, adding the jQuery UI provides you more features for adding different kind of interactivity on your webpage. 2. Angular Since 2016, Angular is becoming a most popular framework for clien-side development. Angular.js is the initial base of Angular (released in 2010) which was completely written in JavaScript. In 2016, Google released the next version of Angular.js as Angular 2.0 (not .js). Angular 2.0 and above versions uses Typescript as a language to write code. Angular added a new way to develop client-side SPA applications. If you want to build a Single Page Application, if you want a most secured application...

Search using Javascript: Part - 1 Searching the text from html table

Hi all, Here is my first programming stuff. This demo is about implementing search operation on your webpage. Here is the source code: HTML and JavaScript: <!DOCTYPE html> <html> <head> <title>Searching the text html table </title> <style>  table{border: 1px solid #000 }  td{border: 1px solid #000; font-size: 20px; padding: 10px; }  tr:nth-of-type(even){background-color: yellow; } </style> </head> <body> <h3> Table</h3><p></p> <table class="tabl" name="tabName1" id="tabId1"> <tr>  <th class="tdClassname"> Name </th> <th class="tdClassname"> Acc No </th> <th class="tdClassname"> Branch No </th> <th class="tdClassname"> Tel </th> </tr> <tr>  <td class="tdClassname"> Name 11 </td> <td ...