Search code examples
javascriptjqueryhtmlmailto

How to add html <br> tags into javascript


I have a button that when clicked opens email and fills the body with variables. This looks too messy though as it all appears on one line. Could I just chuck in a br tag inbetween each variable so they appear on a new line?

Thanks. Here is code:

$("#buttonLink").click (function() {
            window.open('mailto:[email protected]?subject=My Frame&body=My frame Type: ' + frameType + ' My Frame Background: ' + frameBackground + ' My text: ' + yourText + ' My Text design: ' + textDesign); 
        });

I'm thinking something like:

$("#buttonLink").click (function() {
        window.open('mailto:[email protected]?subject=My Frame&body=My frame Type: ' + frameType + <br> + ' My Frame Background: ' + frameBackground + <br> + ' My text: ' + yourText + <br> +' My Text design: ' + textDesign); 
    }); 

Solution

  • Use the ASCII code which is %0D%0A, like this:

    window.open('mailto:[email protected]?subject=My Frame&body=My frame Type: ' + frameType +'%0D%0A My Frame Background: ' + frameBackground + '%0D%0A My text: ' + yourText + '%0D%0A My Text design: ' + textDesign); 
    

    Strictly speaking should the white spaces also be replaced. Use the ASCII code which is %20, like this:

    window.open('mailto:[email protected]?subject=My%20Frame&body=My%20frame%20Type:%20' + frameType +'%0D%0A%20My%20Frame%20Background:%20' + frameBackground + '%0D%0A%20My%20text:%20' + yourText + '%0D%0A%20My%20Text%20design:%20' + textDesign);