Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created February 13, 2013 05:01
Show Gist options
  • Save rainyjune/4942413 to your computer and use it in GitHub Desktop.
Save rainyjune/4942413 to your computer and use it in GitHub Desktop.
Get or set an attribute for a DOM element
function attr(e, n, v) {
var fixAttr = {
tabindex: 'tabIndex',
readonly: 'readOnly',
'for': 'htmlFor',
'class': 'className',
maxlength: 'maxLength',
cellspacing: 'cellSpacing',
cellpadding: 'cellPadding',
rowspan: 'rowSpan',
colspan: 'colSpan',
usemap: 'useMap',
frameborder: 'frameBorder',
contenteditable: 'contentEditable'
};
var d = document.createElement("div");
d.setAttribute("class", "yuanJS");
var fullSupported = d.className === "yuanJS";
if(e && n && v) {
e.setAttribute(fullSupported ? n : (fixAttr[n] || n), v);
} else if (e && n) {
return e.getAttribute(fullSupported ? n : (fixAttr[n] || n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment