Ryan
Ryan

Reputation: 10039

Javascript: Possible to get newlines?

My code is something like this:

function attr(el, attr, value) 
{
    el.setAttribute(attr, value);
}

    var f2=document.createElement("form");
        attr(f2, 'id', "det_"+tresult_no);
        attr(f2, 'name', "form"+tresult_no);

   var t2=document.createElement("TABLE");
    attr(t2, 'width', 80);
    attr(t2, 'border', 0);
f2.appendChild(t2);

then to see/confirm what I am doing is right, I do this:

document.getElementById('testing').value=t.innerHTML;

where "testing" is a textarea.
This works out pretty well... except with a lot of code, the textarea gets filled up and is abit hard to read.

Is there anyway to get some line breaks in the textarea after everytime I add one object to another?

Thanks!

Upvotes: 0

Views: 108

Answers (2)

jfriend00
jfriend00

Reputation: 707148

Assuming the "testing" object is an input or textarea field that displays text and newlines, how about something like this?

document.getElementById('testing').value=t.innerHTML.replace(">", ">\n");

or to just target a newline after closing tags:

document.getElementById("result").value = html.replace(/<\/[^>]+?>/g, "$&\n"); 

Note, this isn't a perfect regex for matching closing HTML tags, but for the purposes of debugging, it should work fine.

Upvotes: 1

Brett Walker
Brett Walker

Reputation: 3576

Try something like

document.getElementById('testing').value= '<br/>' + t.innerHTML;

This start each addition on a new line.

Upvotes: 0

Related Questions