Created
January 14, 2015 14:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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