24

I'm trying to set attribute value that contains a single quote:

var attr_value = "It's not working";
var html = "<label my_attr='" + attr_value + "'>Text</label>";
$('body').html(html);

However, I get the following result:

<label working="" not="" s="" my_attr="It">Text</label>

How could I fix this ?

Are double quotes allowed inside attribute values ?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

4 Answers4

26

Yes, both quotes are allowed in attribute values, but you must HTML-escape the quote you're using as an attribute value delimiter, as well as other HTML-special characters like < and &:

function encodeHTML(s) {
    return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;').split("'").join('&#39;');
}

var html= '<label my_attr="'+encodeHTML(attr_value)+'">Text</label>';

However, you are usually much better off not trying to hack a document together from HTML strings. You risk bugs and HTML-injection (leading to cross-site-scripting security holes) every time you forget to escape. Instead, use DOM-style methods like attr(), text() and the construction shortcut:

$('body').append(
    $('<label>', {my_attr: attr_value, text: 'Text'})
);
bobince
  • 528,062
  • 107
  • 651
  • 834
  • Thanks a lot for a detailed answer! Just for curiosity regarding the implementation of `encodeHTML`: It can be implemented using the `replace` function, right ? Is it less effective ? – Misha Moroshko Oct 22 '10 at 12:36
  • It can, sure, `replace(/&/g, '&')...`. In this case it doesn't matter since the search strings can't contain any regex-special characters, but in general for plain string-replace `split`/`join` can be simpler, since you can use search strings without have to worry about regex-escaping. Comparative performance varies across browsers. – bobince Oct 22 '10 at 12:57
  • Thanks ! Could you give me a pointer to "construction shortcut" tutorial ? – Misha Moroshko Oct 22 '10 at 13:34
  • As an example of @bobince’s point that you shouldn’t hack a document together from HTML strings: the backtick character ``` (which is not included in the `encodeHTML` function) can be used to escape from an [unquoted attribute value](http://mathiasbynens.be/notes/unquoted-attribute-values) in IE. For this reason, it has been made invalid to use the backtick character in an attribute value as per “HTML5”. – Mathias Bynens May 31 '12 at 10:21
25

You can use single quotes inside double quotes or double quotes inside single quotes. If you want to use single quotes inside single quotes or double quotes inside double quotes, you have to HTML encode them.

Valid markup:

<label attr="This 'works'!" />
<label attr='This "works" too' />
<label attr="This does NOT \"work\"" />
<label attr="And this is &quot;OK&quot;, too" />
CodeTwice
  • 2,926
  • 3
  • 18
  • 18
4

Use double-quotes as the attribute delimiter:

var html = "<label my_attr=\"" + attr_value + "\">Text</label>";
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
3
var attr_value = "It&#39;s not working"
Marco Mariani
  • 13,556
  • 6
  • 39
  • 55