Search code examples
javascript

How to add a new line in textContent in javascript


I have the following method -

    function callBack(data1, predicted_label){
        const resultParagraph = document.getElementById("resultParagraph"); 
        resultParagraph.textContent = `Sentiment: ${predicted_label}`;
        const historyList = document.getElementById("historyList");
        const listItem = document.createElement('li');
        listItem.textContent = `Text: ${data1} Sentiment: ${predicted_label}`;
        historyList.prepend(listItem);  
    }

I want to add a new line between Text: ${data1} and Sentiment: ${predicted_label}.

I've tried adding \n, tried adding a <br> tag but nothing seems to work.


Solution

  • HTML rendering doesn't respect newlines - you'll need to use innerHTML instead of textContent and add a <br> tag where you want the line break