Line Breaks

This is probably pretty easy, I just can't figure it out.  I have a string that I'm reading in from innerHTML... when I look at the string in the source code it's got breaks all over the place.  though when it's in the HTML there's no breaks.

I tried a ...innerHTML.replace("\n", "<br />") but it didn't seem to work.  what's the best way to do this?
BeamerColaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arantiusCommented:
var str=element.innerHTML;
str.replace("/\n/", "<br />");
element.innerHTML=str;

Untested, but I'm quite sure that will work for you.  Let me know =)
0
knightEknightCommented:
or:

str = str.split("\n").join("<br />");
0
knightEknightCommented:
in any event, you will need to assign the results:

element.innerHTML = element.innerHTML.replace( "\n", "<br />" ) ;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:

<form>
  <p><textarea name="t1" cols="60" rows="10">
This is some text
with a few line breaks

Sometimes more than one
      sometimes with some extra spaces for indentation
And somtines not but space      between words for formatting.
</textarea></p>

<input type="button" value="Add to page"
   onclick="document.body.innerHTML+=t1.value.replace(/\n/g,'<br>').replace(/  /g,'&nbsp;&nbsp;&nbsp;&nbsp;'); ">
<input type="button" value="View HTML" onclick="alert(document.body.innerHTML) "> </p>
</form>
0
BeamerColaAuthor Commented:
okay hmm, that didn't seem to work.  Let me explain my situation a little more.

I've got a frameset, and the bottom frame is uneditable, the top frame is though.  So in the top frame I'm using javascript to format the uneditable frame.

Here's the source of the bottom frame:
-----
No problem, I will transfer the additional money into you
r acct today.
 
Thanks.
 
Vicky
-----

and my script:
parent.BODY.document.body.innerHTML = parent.BODY.document.body.innerHTML.replace("/\n/", "<br />");

I think i'm doing it wrong, and I need to replace whatever character the breaks are with a \n character.  Maybe I'm wrong ...
0
knightEknightCommented:
.replace("\n", "<br />");
0
knightEknightCommented:
instead of:

.replace("/\n/", "<br />");
0
BeamerColaAuthor Commented:
still no luck =/
0
knightEknightCommented:
is there an error message?
0
knightEknightCommented:
your script is in the top frame, and the bottom frame is named "BODY" , correct?
0
knightEknightCommented:
and the pages in both frames are loaded from the same domain (server), correct?
0
BeamerColaAuthor Commented:
correct
0
knightEknightCommented:
are there any HTML tags in the source of the bottom frame?
0
BeamerColaAuthor Commented:
<html><body><pre>[message]</pre>

unfortunately the craphead didnt close html or body.
0
knightEknightCommented:
yikes, that could cause the problem ...

What do you see if you do this?

alert( parent.BODY.document.body.innerHTML );
0
BeamerColaAuthor Commented:
all the text that I'm trying to alter
0
GwynforWebCommented:
what you have in the source code and what the innerHTML is are in general  different
0
arantiusCommented:
Are the two frames pages loaded from the same domain?  If you are trying to alter a page loaded from another domain, you cannot.  Security based limitations.
0
BeamerColaAuthor Commented:
Yeah they're both on the same domain.  Good point though.
0
arantiusCommented:
Well I am unsure what exactly is happening here so...

var str=document.frames.frameName.document.body.innerHTML;
str.replace("/\n/", "<br />");
alert(str);
document.frames.frameName.document.write(str);

Replace frameName above with the actual name of the frame.
What is alerted?  What shows up in the frame?
0
knightEknightCommented:
why are you enclosing the newline character in frontslashes?
should be:

str = str.replace("\n", "<br />");
0
knightEknightCommented:
or:
str = str.split("\n").join("<br />");
0
arantiusCommented:
Because I'm juggling too many regex implementations in my head and I never properly tested this post =)  I was thinking this:

str=str.replace(/\n/, "<br />");

Which I just tested working.  Quotes or slashes works, but not both!
0
knightEknightCommented:
ah, yes I see now, but it should be:

str=str.replace(/\n/g, "<br />");

:D
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GwynforWebCommented:
please note

THE SOURCE CODE AND INNER HTML ARE NOT IN GENERAL THE SAME!!!!

 Extra spaces are stripped by the interpreter, else this would have had done the task http:Q_21217645.html#12658579
0
BeamerColaAuthor Commented:
yes yes yes KnightEknight!  Thanks!
0
knightEknightCommented:
thanks, but it was a group effort.  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.