Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to put line break while showing error message using jquery ?
below is my code.

for(var i in testids)
{
singid= singid+testids[i]+", ";
}

$("#errmsg").text(singid);

i want to display ids in separate lines, ie instead of comma as a separator i want to put line break.
i tried
singid= singid+testids[i]+"
";
singid= singid+testids[i]+"\n";
singid= singid+testids[i]+"\b";
singid= singid+testids[i]+"
";
but no luck.
anybody knows how to put break ?
Posted
Comments
Sergey Alexandrovich Kryukov 19-Oct-15 16:40pm    
It all depends on where you display the resulting string. Where?
And don't try, just do, based on knowledge.
—SA

1 solution

Since the output is HTML, you need to use the <br> tag[^]:
JavaScript
singid = singid + testids[i] + "<br>";

However, you're using the text method to display the error message, which will automatically HTML-encode the output, including the <br> tag.

Instead, you'll need to HTML-encode each error message, and then use the html method to display the error:
JavaScript
function htmlEncode(value){
    return $("<div/>").text(value).html();
}

...

for (var i in testids)
{
    singid = singid + htmlEncode(testids[i]) + "<br>";
}

$("#errmsg").html(singid);
 
Share this answer
 
v2
Comments
Am Gayathri 19-Oct-15 11:45am    
this will not work. i tried already
Richard Deeming 19-Oct-15 11:46am    
See the updated answer - you need to change where you're HTML-encoding the output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900