Skip to content

Instantly share code, notes, and snippets.

@mvark
Created January 14, 2015 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvark/c8818404b88f79fda9aa to your computer and use it in GitHub Desktop.
Save mvark/c8818404b88f79fda9aa to your computer and use it in GitHub Desktop.
JavaScript function to handle quotes within injected text assigned to Title attribute of a HTML element
Before:
elem.append('<a title="'results[i].title
+ '" href="http://www.youtube.com/embed/' + results[i].id + '?autoplay=1&vq=small&autohide=2&iv_load_policy=3" >' + results[i].title + '</a>');
function htmlEscape(str) {
return String(str)
.replace(/'/g, ''')
.replace(/"/g, '"');
}
After:
elem.append('<a title="'htmlEscape(results[i].title)
+ '" href="http://www.youtube.com/embed/' + results[i].id + '?autoplay=1&vq=small&autohide=2&iv_load_policy=3" >' + results[i].title + '</a>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment