title: How to get MVC Model Datatype String with Newline in Javascript
body: <p>I am attempting to pass the data (type = string) from my MVC model to a javascript funcion but I keep getting an error "Uncaught SyntaxError: Unexpected token ILLEGAL". I believe this is caused by the newlines in the model data as it only occurs with multi-line strings.</p>
  
  <p>The idea is for the user to press the "undo" button/link to reset the text area.</p>
  
  <p>Image:</p>
  
  <p><img src="https://i.stack.imgur.com/J2VRP.png" alt="enter image description here"></p>
  
  <p>CSHTML:</p>
  
  <pre><code>@Html.TextAreaFor(model =&gt; Model.updates[i].body, new { @Class = "txtTextArea floatLeft", @Id = "Edit_" + i.ToString(), @style = "width: 70%;", @Rows = "4", @Cols = "30" })
  
  &lt;a class="textRight EditToLink" onclick="clearInput('Edit_@i.ToString()', '@Html.Raw(Model.updates[i].body)')" id="EditToOneLink_@i.ToString()" href="#" &gt;undo&lt;/a&gt;
  </code></pre>
  
  <p>Javascript Function:</p>
  
  <pre><code>function clearInput(input, val) {
      if (document.getElementById(input) != null) {
          if (val != null) {
              val.replace(/\r?\n/g, "\r\n");
              document.getElementById(input).value = val;
          }
          else {
              document.getElementById(input).value = "";
          }
      }
  }
  </code></pre>
  
  <p>Similar Question:
  <a href="http://forums.asp.net/t/1741372.aspx?pass+model+parameter+to+javascript+function" rel="nofollow noreferrer">http://forums.asp.net/t/1741372.aspx?pass+model+parameter+to+javascript+function</a></p>
  
  <p>Thanks is advance!</p>