Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

altough test for '\n' is false, it persists in code output!

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
0
0

dang...this is funny...i have been trying to get this code to work:

****
<html>
<head>
<title>Line Breaks Part II</title>

<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>

function addbr() {
txt = '';
mytext = document.getElementById('mytext');
val = mytext.value;
if (val!='') {
val2 = val.split(&quot;\n&quot;);
val3 = val2.length;
if (val.indexOf('\n')!=-1) {
document.getElementById('submit').disabled = true;
for (s=0; s<val3; s++) {
brk = '<br>';
ln = '\n';
if (s==0) {txt = '';}
if (!val2=='') {txt = txt+val2+brk+ln;}
}
mytext.value = txt;
}
}
mytext.focus();
}

</script>

</head>
<body>
<form name=&quot;myform&quot; action=&quot;javascript:addbr();&quot;>
<textarea name=&quot;mytext&quot; cols=&quot;50&quot; rows=&quot;8&quot;></textarea>
<br>
<input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>
****

for some reason it carries the carriage return with each element in array val2...i have tested for the presence of \n in each array item, but it returns false. yet one exists in the output, or so it seems. there is purposely a \n after the <br>, but not after the val2 array item, although it line breaks anyway...i can't it to work, so i give it to the group.

if input looks like this:

line 1
line 2
line 3

output should look like this:

line 1<br>
line 2<br>
line 3<br>

any ideas?

- g
 
yes try to check for \\n instead...

eg...

... code ..

brk = '<br>';
ln = '\\n';

.....

good luck

matt




 

adding that extra \ only cause it to be interpreted literally, so in the output you actually see the \n...

you actually have to copy & paste the code and run to see what i mean...

or am i misunderstanding your reply?

any other ideas?

- g
 
if (!val2=='')

try this instead

if( val2 != '')


-pete
 
nope...can anyone make this code work? maybe i'm doing something wrong, but in the code, each entry into text box that has a hard return becomes an item in an array that is split up by each \n...that part works fine, but when i use each item as a variable, it keeps the hard return and shows in output...i tested for the \n presence, but returned false.

the code is above...i don't even need this for anything right now, but it's bothering me...couldn't figure out and i tried for so long...

- g
 
Hmm, how about this: (warning, my js is a little rusty)
Code:
<html> 
<head> 
<title>Line Breaks Part II</title>

<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;> 

function addbr() {
	var txt;
	txt = document.getElementById('mytext').value;
	document.getElementById('mytext').value = txt.replace(/\n/g,&quot;<br>&quot;);
}

</script>

</head> 
<body> 
<form name=&quot;myform&quot; action=&quot;javascript:addbr();&quot;> 
<textarea name=&quot;mytext&quot; cols=&quot;50&quot; rows=&quot;8&quot;></textarea>
<input type=&quot;button&quot; onClick=&quot;addbr();&quot; value=&quot;Test Button&quot;>
<br> 
<input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Submit&quot;> 
</form> 
</body> 
</html>

This may be a bit off but should be close,

-Tarwn [sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 

nope again!

if the input looks like this:

if input looks like this:

line 1
line 2
line 3

output should look like this:

line 1<br>
line 2<br>
line 3<br>

even with your example the output is this:

line 1
<br>line 2
<br>line 3

i don't even need this code (yet!) but i am interested in why my code (see above) will not work. it bothers me i can't figure it out.

- g
 
ahh.. sorry bout before mustn't have had my coding head on.

split() will not only split the string, but also return the split char at the end of the return string. this is why, when you're adding the strings up:

txt = txt+val2+brk+ln;

val2 would be &quot;line1\n<br>\nline2\n<br>\nline3\n<br>\n&quot;

because val2 would always be &quot;linex\n&quot;!

however if you do this:


txt = txt+val2.substring(0, val2.length - 1)+brk+ln;


it works.


What intrigues me more is that the regular expression Tarwn used didnt work either! now that IS wierd.


matt


 
If you're interested I could help you rewrite it. Just tell me what you want the code to do. If I'm not mistaken you want it to add HTML breakline tags before returns; is that right?

--------------------------------------
if(Me.Hungry!=false){Me.Eat(&quot;Pizza&quot;);}

-Jason
 

yeah, but i don't really need a code that works. i more want this code to work. in theory and just looking at it, it looks right. but it's not.

- g
 
did u even read what i wrote? *poke*

its cause split splits the string and includes the split char when it returns.. just take it off.
 
Hi Spewn,
I know you said you didn't want a new code (what programmer does?). But I was board and I decided to write it anyway. If you do give up, by any chance, feel free to use this code; it's more simple anyway.

- SE

function AddBR(UnformattedText){
var CurrentPosition=0;
var ReturnString='';

while(CurrentPosition<UnformattedText.length){
if(UnformattedText.substr(CurrentPosition,2)=='\r\n'){
ReturnString+='<BR>';
}

ReturnString+=UnformattedText.substr(CurrentPosition,1);
CurrentPosition++
}

return ReturnString;
}
 

flumpy:

tried to replace my txt=... with your new expression and get an error. however you did address the question and came up with a solution. i see what you are doing...what's weird is after the split, i even tested for the existence of the \n and it came up false! but what's up with the error?

SE:

thanks for the help. i don't have a practical use for the code, (although one will come in the future, undoubtedly)...i just couldn't figure out the reason it didn't work. you know how that is...it just bugs and bugs...no pun intended.

- g
 
I understand. I'm at a level where I shouldn't make any mistakes in syntax, A code that doesn't work is enough to drive me to insanity.
 
Spewn:

i missed out the array index.. it should be:

txt = txt + val2.substring(0, val2.length - 1) + brk + ln;


soz.

matt

ps wtf is up with this strikethrough text?

 
oh so it wasnt me.. it was the fricking board editing out my code...!!

again i say it should be

txt = txt + val2\[s\].substring(0, val2.length) + brk + ln;


remove the \'s
 

dang...finally! you're idea was correct for the substring()!! this drove me crazy (further, at least!).

here's the code from the for loop on...

**
for (s=0; s<val3; s++) {
val4=val2.split(&quot;&quot;);
brk = '<br>';
ln = '\n';
num=val2.length-1;
if ((s+1)==val3) {brk='';ln='';num=val2.length;} //#1
if (s==0) {txt = '';}
if (!val2=='') {txt = txt + val2.substring(0,num) + brk + ln;}
}
**

i added #1 in there so the last line doesn't get a <br> and hard return if the user didn't hit return, and the 'num' variable so the last character of the last entry doesn't get deleted.

thanks for your help! very persistent. good trait/quality to have.

i'm gonna post it to the original post in case the user is still stuck.

*oh yeah, this post had strikethrough as well, then i unchecked the 'process tgml' box and no problems.

- g
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top