Jump to content

help with changeing \n to <br>.....


meomike2000

Recommended Posts

in my script i turn a string that was entered into a textbox in to a textnode with js.....

 

my problem is when i dispaly that textnode al the returns or \n s are ignored......

 

so if i had this in the textbox input:

 

test.....

 

test2....

 

when i display that with the js it comes out like this

 

test.....test2.....

 

i have tried to use replace('\n', '<br />'); but now it looks like this:

 

test.....<br />test2.....

 

can anybody help please........

Link to comment
https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/
Share on other sites

ok i solved the problem, what i did was split the string at the \n. then i replaced all the \n with a div tag and that fixed the problem for me...

 

also on the lines where there was no text i had to set the div height...

 

i also have this set so that when you click on a post you can then click edit and it will then strip all the div. and replace them with \n so that it will display correctly inside a textbox.....

 

thanks to all that looked.......

 

here is how i add the div tags....

var newpost = post.split('\n');
for ( var b = 0; b < newpost.length; b++) {
	var postdiv = document.createElement('div');
	if (newpost[b] == '') {
		postdiv.style.height = '10px';
	}
	postdiv.appendChild(document.createTextNode(newpost[b]));
	//mbpost.appendChild(document.createTextNode(newpost));
	mbpost.appendChild(postdiv);
}

 

and how i strip them....

var splitpost = mbpost.innerHTML.split('</div>');
var postinfo = mbpost.innerHTML;
for (var c = 0; c < splitpost.length; c++) {
postinfo = postinfo.replace('<div>','').replace('<div style="height: 10px;">','').replace('</div>','\n');
}

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.