Web Analytics Made Easy -
StatCounter replace doesn't work ?? - CodingForum

Announcement

Collapse
No announcement yet.

replace doesn't work ??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Code:
    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:
    Code:
    result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;
    and in my html:
    Code:
    <div id='waarde' style="width:500px;"></div>

  • #2
    I'm sorry but I really do not understand,
    however
    result.waarde.replace('mdw', 'chr(13)');container.innerHTML = result.waarde;
    should be ...
    container.innerHTML = result.waarde.replace('mdw', 'chr(13)');
    hope this helps ?

    Comment


    • #3
      To strip newlines:-

      Code:
      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.

      Comment


      • #4
        I don't understand how the code result.waarde is able to refer to the div identified as
        Code:
        <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.

        So...

        I'd guess that what's really needed is:
        Code:
        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.
        Be yourself. No one else is as qualified.

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎