0

hello i have a this code but i have a problem with quotes in javascript:

var content = 'peter says: 'hello'';

and

var append = '<div class="'+content+'>';

how proteted quotoes ?? attribtties class only show: peter says: thanks and sorry for my english.

user2799505
  • 37
  • 1
  • 5
  • replace quotes in text that you're inserting into attributes with their entity equivalents. `'` for `'` and `"` for `"` – Kevin B Oct 03 '13 at 15:06
  • Keep in mind that javaScript allows different types of quotes ' ' and " " take a read over this: http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript it talks about it some. Also http://www.w3schools.com/js/js_obj_string.asp is quite informative. – buzzsawddog Oct 03 '13 at 15:07

3 Answers3

1
var content = "peter says: 'hello'";

Escape the quotes.

This would work as well (put a back slash in front of the quote you want to display):

var content = 'peter says: \'hello\'';
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
1

You can do this by escaping it using \"

var content = 'peter says: \"hello\"';

However this should work

var content = 'peter says: "hello"';

To support, I have added JSFiddle

There is no need to escape double quotes within single quotes or vice versa. However escaping is a standard approach.

Here I got a reference too

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • @user2799505 I have added few details and reference. Though you already got your answer, I would like you to take a look at the reference for a good understanding. – Praveen Oct 03 '13 at 15:22
0

Is that fiddle answers your question

<div id="test">

</div>


$(function()
  {
      var content = "petersays: 'hello'";
      var classToAppend = "class=\""+content+"\"";
      var append = "<div +classToAppend+">test</div>";
     $("#test").append(append);      
  });

http://jsfiddle.net/29fLy/2/

Heaven42
  • 329
  • 1
  • 13