Skip to content

Instantly share code, notes, and snippets.

@meghuizen
Created April 18, 2013 08:44
Show Gist options
  • Save meghuizen/5411233 to your computer and use it in GitHub Desktop.
Save meghuizen/5411233 to your computer and use it in GitHub Desktop.
Escape HTML attributes using JavaScript
/**
* Escapes HTML attributes using JavaScript
*/
function escapeHtmlAttr(attr) {
var cdiv = document.createElement('div');
cdiv.setAttribute('abc', attr);
return cdiv.outerHTML
.substr(10, cdiv.outerHTML.length - 18);
//.replace(/^<div abc="/i, '')
//.replace(/"><.div>$/i, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment