-3

I am getting when use below jQuery code. I don't understand what is the problem is?

$("<table cellspacing="0" width="100%" class="ms-rteTable-default"><tbody><tr>
  <td class="ms-rteTable-default" style="width: 50%;"></td></tr></tbody></table>")
 .appendTo("#Business_x0020_Benefits_7c71784a-a647-4338-a871-27a49740ee27_$TextField_inplacerte");
James123
  • 9,918
  • 51
  • 172
  • 316
  • 4
    Holy cow. It's impossible to understand something. Just format it properly. – Andrew Dunai Dec 11 '14 at 23:25
  • If you have a good number of these types of strings you might want to think about using html partials and just load them in (thru ajax or other methods) when needed. – Patrick Evans Dec 11 '14 at 23:31
  • possible duplicate of [Double quote in JavaScript string](http://stackoverflow.com/questions/10055773/double-quote-in-javascript-string) – Patrick Evans Dec 11 '14 at 23:35
  • now I am getting error at `Uncaught Error: Syntax error, unrecognized expression: #Business_x0020_Benefits_7c71784a-a647-4338-a871-27a49740ee27_$TextField_inplacerte` – James123 Dec 12 '14 at 16:01
  • I updated my answer. you can't use $ in your id. – Kai Qing Dec 12 '14 at 16:38

3 Answers3

6

You must escape quotes (") inside strings in JavaScript:

// This is WRONG!
$("<div class="foo">bar</div>");

// This is correct.
$("<div class=\"foo\">bar</div>");

See? Even the StackOverflow syntax highlighter tries to help you :)

Andrew Dunai
  • 2,784
  • 16
  • 26
3

$("< you are starting your statement with the same quotes you're using inside the html.

change that to this:

$('<table cellspacing="0" width="100%" class="ms-rteTable-default"><tbody><tr>
  <td class="ms-rteTable-default" style="width: 50%;"></td></tr></tbody></table>')
 .appendTo("#Business_x0020_Benefits_7c71784a-a647-4338-a871-27a49740ee27_$TextField_inplacerte")

Using a single quote string literal is acceptable and makes it easier to manage if you're using double quotes inside the statement. If you have single quotes in there, you'll need to escape them with \' the same way the other answers are saying. Andrew's answer is perfectly valid as well. It just means you'll have to escape everything.

EDIT

You're getting a syntax error because that $ in your ID. you can't use that symbol in an html id. See this post: Which characters are valid in CSS class names/selectors?

Community
  • 1
  • 1
Kai Qing
  • 18,359
  • 5
  • 34
  • 56
3

It's just a syntax error, you have to escape your " with \"

Freez
  • 5,367
  • 2
  • 16
  • 26