0

I have a small issue I'm running into when I try to prepopulate an email body with the contents of a div. Right now, I have a button set up that opens the email client with the information prepopulated. Everything is working perfectly, except for the fact that instead of recognizing the br as a line break, text just show up in one line with literal brs separating each "line":

enter image description here

Here is my code:

            $(function () {
                $('.SendEmail').click(function (event) {
                    var email = '';
                    var subject = 'i wrote you a poem';
                    var emailBody = document.getElementById("hellomessage").innerHTML;
                    document.location = "mailto:"+email+"?subject="+subject+"&body="+emailBody;
      });
    });

I think this is because, in the other function I have that populates the contents of this div, I use the following to add a line break between each line:

document.getElementById("hellomessage").innerHTML += nodes[randomNum-1].innerText;
document.getElementById("hellomessage").innerHTML += "<br>";

How can I fix this? I want the system to understand that br indicates a new line, so I can have each chunk of text show up on a new line within the body of the email.

Thank you in advance!

  • https://stackoverflow.com/questions/22765834/insert-a-line-break-in-mailto-body may include the answer. – RK234 Mar 27 '23 at 18:37

1 Answers1

0

When you just want to replace the <br> tags with a newline you could do RegExp replace.

let content = document.getElementById("hellomessage").innerHTML;
content = content.replace(/<br>/g, "\n");
// To remove the last newline, use:
content = content.trim()

Depending on how your email client treats the mail this might work.