md10 md10 - 3 years ago 211
jQuery Question

Not able to print <> brackets in XML output in html

So this is with regards to one of my previous questions, where in i wanted to replace "\n" with a "br" tag in HTML using JS.
For the same page, if someone passes a XML or RPC query, i want to return an XML output.

A typical XML output looks like:


**I want to be able to print it in this way: (replacing \n with a br)



But i am only getting the plain text in the output, which is 0 and XYZ789 with so many \n's.

The JS:

$(function() {
$('button').click(function() {
url: '/runCommand_query',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
response = response.replace(/(\n)/g, "<br>");
error: function(error) {

Answer Source

You should use text() instead of html() like :


Else the browser will interpret the code as HTML tags, and shows you just the text.

Hope this helps.


var response = "<racks>\n<rack>\n<rack-name>0</rack-name>\n<chassis>\n<serial-number>XYZ789</serial-number>\n</chassis>\n</rack>\n</racks>\n</diag>\n</data>\n</rpc-reply>\n";

$('#opt').text(response).wrap('<pre />');
<script src=""></script>

<div id="opt"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download