a function like nl2br but in javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gozil
    New Member
    • Jul 2007
    • 42

    a function like nl2br but in javascript?

    Hello, was just wondering if its possible to have a function like nl2br in javascript. I need it to change the row where the user have used enter.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Gozil.

    If you think about it, nl2br() is really just a fancy way of doing this:
    [code=php]
    str_replace("\n ", "<br />\n", $str);
    [/code]

    It's actually slightly more complicated than that, but the point is the same.
    With that in mind, you can run a quickie string replace of your own:

    [code=javascript]
    var $textarea = document.getEle mentById('theTe xtarea');

    $textarea.value = $textarea.vaue. replace('\n', '<br />\n');
    [/code]

    Comment

    • Gozil
      New Member
      • Jul 2007
      • 42

      #3
      Thanks, but i need to use it in a xml file and if I use <br /> in another xml tag i wont get the whole node. But I could use something like [br] and then convert it to <br /> later. But im not sure how to convert back a text that uses [] I get it wrong when I write this:
      Code:
      var my_string = 'hello this is a text[br]this is the next row';
      my_string = String.replace(my_string,/[br]/g,'<br />');
      it doesnt replace the whole [br] tag..

      edit:
      nvm I can just use this when i try to output the text:
      my_string = String.replace( my_string,/[\n]/g,'<br />');

      instead of replacing the \n before i put it in the mySQL database.

      Comment

      Working...