0

What I am doing:

I am storing (MySQL) formatted text that is structured like this:

I'm Robert (I'm = I am)<br>You're in France (You're = You are)<br><br><strong></strong><span class="st">?</span> <b>I</b>'m Robert  <br>? Am Robert <del> <br></del><br>? <b>You</b>'re Robert <br>? Are Robert  <br><br>I'm = I am<br>You're = ___ ___?<br><br><br><br>

I am trying to retrieve this string from the database an then append it into a WYSIWYG editor like this.

function enableEditMode(){
            card_content.document.designMode = 'On';
            $('iframe[name=card_content]').contents().find('body').append("I'm Robert (I'm = I am)<br>You're in France (You're = You are)<br><br><strong></strong><span class="st">?</span> <b>I</b>'m Robert  <br>? Am Robert <del> <br></del><br>? <b>You</b>'re Robert <br>? Are Robert  <br><br>I'm = I am<br>You're = ___ ___?<br><br><br><br>");
}

I get the following error:

SyntaxError: missing ) after argument list

I assume this is because of the "" quotes causing issues.

Question:

How do I go about appending HTML into the DOM without encuring syntax errors like this?

Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80

2 Answers2

1

Just replace

class="st"

by

class='st'

should do the trick. I dont see more quote conflicts.

Der Vampyr
  • 941
  • 1
  • 7
  • 13
1

I think you need to escape single quote or double quotes:

function addslashes( str ) {
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

from: Escaping Strings in JavaScript

Or you escape the string in server side and return the escaped string. Therefore, the result should be like this:

"this is a test, isn\'t it great! <span class=\"test\">Test</span>"
Community
  • 1
  • 1
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60