1

I am trying to print the following output onto my html page:

100.101.102.103

Trying 192.168.30.61 ... Open

Warning Notice

This system is restricted solely to company authorized users


But instead i am getting this : '\n100.101.102.103\r\nTrying 100.101.102.103 ... Open\r\n\r\n************* **************************************************************\r\n Warning Notice\r\n\r\nThis system is restricted solely to company authoriz ed users


The JS:

<script>
    $(function() {
    $('button').click(function() {
        $.ajax({
            url: '/runCommand_query',
            data: $('form').serialize(),
            type: 'POST',
            success: function(response) {
            //alert("hi");
            console.log(response);
            //$("#opt").html(response);
            $("#opt").html(response);
        },
        error: function(error) {
        console.log(error);
    $("#opt").val(error);
        }
    });
});

});

The HTML:

output id="opt" name="opt"

I need a solution to get rid of the "\r" and "\n"

2
  • You missed the brackets <> around the output tag in your snippet. Commented Nov 15, 2017 at 20:18
  • Ya i may have missed them while writing this question but not in the actual html. Commented Nov 15, 2017 at 21:18

1 Answer 1

2

You need to convert the JavaScript string escape code for a new line to an HTML <br> element.

At the beginning of your AJAX success handler, add:

response = response.replace(/(\n|\r)/g, "<br>");

Here's an example:

var response = " '\n100.101.102.103\r\nTrying 100.101.102.103 ...";

response = response.replace(/(\n|\r)/g, "<br>");

document.querySelector("p").innerHTML = response;
<p></p>

Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it ;)
I have come across one more issue. Wondering if you could help: In the below XML example, i only want to print the plain text,which is Line Card-1 and ABC123, without the line breaks. ` <rack>\n <rack-name>Line Card-1</rack-name>\n <chassis>\n <serial-number>ABC123</serial-number>\n </chassis>\n </rack>\n </racks>\n </diag>\n </data>\n</rpc-reply>\n ` using the same JS above, how do i modify it to print output without substituting \n by <br> for XML syntax? I am assuming I would need an if statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.