Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Thanked 1 Time in 1 Post

    Question replace doesn't work ??

    I've got a form textarea on my site, which after being submitted is stripped by my php file.

    Then my javascript kicks in and does some analysis of the entered text. However everytime the user entered a hard return within the textarea my javascript gives me an error 'tekenreeks niet afgesloten' which means something like 'string is not closed'.

    Therefore I decided to replace all occurences of chr(10) and chr(13) with
    ok2 (just some random characters), I did this with php which worked fine, and I didn't get the javascript error.

    However I need to reshow the entered text (original) to the user, so I need to restore the old values. I tried
    result.waarde.replace('ok2', 'chr(13)');
    but it doesn't work, as it still displays ok2.

    I'm no expert in javascript, but how can I replace those characters by the hard returns?

    Oh yeah I'll show some more coding, perhaps the error can be found there:
    result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;
    and in my html:
    <div id='waarde' style="width:500px;"></div>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Thanked 212 Times in 210 Posts
    I'm sorry but I really do not understand,
    result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;
    should be ...
    container.innerHTML = result.waarde.replace('mdw', 'chr(13)');
    hope this helps ?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    To strip newlines:-

    var txt = "The text as entered by the user";
    strippedTxt = txt.replace(/\r?\n/g,"");  // remove all newlines
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,738 Times in 4,700 Posts
    I don't understand how the code result.waarde is able to refer to the div identified as
    <div id='waarde' style="width:500px;"></div>
    What is the variable result supposed to be???

    And why would you want the string "chr(13)" in the text of your innerHTML??? Are you thinking the chr(13) will be a newline character, as it would be in VB or VBScript? Nope, no way. In JavaScript, the newline character is "\n"

    Further, the replace method normally takes a regular expression as its first argument. If you use a string, it's automatically converted to a regular expression.


    I'd guess that what's really needed is:
    container.innerHTML = document.getElementById("waarde").replace( /mdw/, "\n" );
    But even that assumes that container is a reference to some DOM element that *has* and innerHTML property.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts