Search code examples
javascriptalert

\n as well as <br> for new line in JavaScript alert not working


I am using a JavaScript alert (a little modified though). This alert has a title field and a content field. In my content field I want to display some text in new line, but neither \n nor <br> is working. See the image to check the output of the given code.

new Attention.Alert({
                title: 'School',
                content: 'Date - '+v_date+'\n School Name - '+ school_name + "\n School City - " + school_city + " "
            });

enter image description here


Solution

  • You just have to add CSS white-space: pre-line; for popup content.

    Here is working Example.

    jQuery(document).ready(function($) {
      $('button').click(function(e){
        e.preventDefault();
        var v_date = '04/11/2019';
        var school_name = 'School Name';
        var school_city = 'School City';
        new Attention.Alert({
            title: 'This is a Title',
            content: 'Date - '+v_date+'\n School Name - '+school_name+'\n School City - '+school_city
        });
      });
    });
    *, *:after, *:before{
    	box-sizing: border-box;
    }
    body {
    	font-family: 'Open Sans', serif;
    	background-color: #d1d8de;
    	padding: 20px;    
    }
    .attention-component .inner-container p{
        white-space: pre-line;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://janmarkuslanger.github.io/attention.js/dist/attention.js"></script>
    <link href="https://janmarkuslanger.github.io/attention.js/dist/attention.css" rel="stylesheet">
    <button>Click</button>