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
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');
}

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.