Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert newline in text displayed in span from javascript

I have a span like this :

<span class "message"></span>

And I am filling it from javascript like this :

message = "first message" + "\\\n";
            message = message + "second message";            
            $(".message".)text(errorMessage);
            $(".message").show();

The problem with that, is when the text is displayed on my browser, the 2 messages are on the same line, I can't insert the second with a newline. Whereas in my debug console, the text appears well on 2 lines. I have also tried the <br>, which is worst as it's not interpreted, so I am getting a message like this :

first message br second message.

Basically, I would like to have displayed :

first message
second message
like image 225
user2443476 Avatar asked Sep 02 '25 06:09

user2443476


1 Answers

You can use <br /> and html() to break the text on new line:

message = "first message" + "<br />";
message += "second message";
$(".message").html(errorMessage).show();

Set the HTML contents of each element in the set of matched elements.

Docs: http://api.jquery.com/html/

like image 113
Tushar Avatar answered Sep 04 '25 20:09

Tushar