Between both:
Javascript
function setCss(object, css) { return (object.className = css); } function getCss(object, css) { return object.className; }
or
function getCss2(object) { if (object.getAttribute("className")) { return object.getAttribute("className"); } return object.getAttribute("class"); } function setCss2(object, cssclass) { if (object.getAttribute("className")) { return object.setAttribute("className",cssclass); } object.setAttribute("class",cssclass); }
HTML
<a href="#" onClick="setCss(this, 'newclass')" /> <a href="#" class="something" onClick="alert(getCss(this))" /> <a href="#" onClick="setCss2(this, 'newclass')" /> <a href="#" class="something" onClick="alert(getCss2(this))" />
Both versions work in IE8, FF4, Chrome, Opera and Safari. ( jsFiddle (enhanced) demo )
Which of the best use practices and why? Have you ever encountered any problem with any version?
source share