meomike2000 Posted January 2, 2010 Share Posted January 2, 2010 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........ Quote Link to comment https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/ Share on other sites More sharing options...
meomike2000 Posted January 3, 2010 Author Share Posted January 3, 2010 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/#findComment-987462 Share on other sites More sharing options...
bibby Posted January 4, 2010 Share Posted January 4, 2010 Using divs seems a little heavy handed. document.createElement('br') is perfectly reasonable, and exactly what you were initially asking for ("nl2br"). Working example @ http://bbby.org/u/w Quote Link to comment https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/#findComment-987983 Share on other sites More sharing options...
Zane Posted January 4, 2010 Share Posted January 4, 2010 there is a function for this called nl2br It's short for newline2break Quote Link to comment https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/#findComment-988080 Share on other sites More sharing options...
meomike2000 Posted January 20, 2010 Author Share Posted January 20, 2010 when i try that it literaly prints that out on the screen..... still dont perform the line break or the return...... so i will have to stick to the div method for now, will toy with it some, thanks Quote Link to comment https://forums.phpfreaks.com/topic/186973-help-with-changeing-n-to/#findComment-998985 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.