Skip to content

Instantly share code, notes, and snippets.

@williammalo
Forked from 140bytes/LICENSE.txt
Last active January 27, 2018 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williammalo/2820283 to your computer and use it in GitHub Desktop.
Save williammalo/2820283 to your computer and use it in GitHub Desktop.
Complete jquery css method clone

A perfect replica (hopefully) of the jQuery .css() method

testNode.css("color","blue")   //use it with two string arguments!

testNode.css({color:"red"})    //use it with an object!

testNode.css("color")          //make it return the value of a property

It's even chainable!

Credit to @atk for the great minifiing tips!

function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):getComputedStyle(this)[a]}
function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):getComputedStyle(this)[a]}
//if you want old ie support:
function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):(this.currentStyle||getComputedStyle(this))[a]}
//faster production ready code
Element.prototype.css = function(a,i,n){
if(i){this.style[a]=i;return this}
if(!i&&typeof a==="object"){for(n in a)this.style[n]=a[n];return this}
return this.style[a]||getComputedStyle(this)[a]
}
{
"name": "jQueryCss",
"description": "A replica of the jQuery .css() method",
"keywords": [
"dom",
"style",
"css",
"jQuery",
"method"
]
}
<!DOCTYPE html>
<title>Foo</title>
<a>Error!</a>
<script>
Element.prototype.css = function(a,i,n){for(n in''+a===a||a)this.style[n]=a[n];return i||n?(this.style[a]=i,this):getComputedStyle(this)[a]}
testNode = document.getElementsByTagName('a')[0];
testNode.css("color","blue")
.css({color:"red"})
testNode.innerHTML=testNode.css("font") //even works with computed styles!
</script>​
@atk
Copy link

atk commented May 29, 2012

Shortened string detection using coercion: ''+a===a instead of typeof a=='string'

Update: even shorter version by using negative string detection and non-parsing of number object 0:

function(a,i,n){for(n in''+a!==a?a:0)this.style[n]=a[n];return n||i?((this.style[a]=i),this):this.style[a]}

However, you are aware that this will only get inline styles, not currentStyle (IE) nor getComputedStyle (W3C) informations?

@williammalo
Copy link
Author

@atk
Thanks! Thats a smart bit of code there!
I updated it to get the computed styles as well.

@atk
Copy link

atk commented May 29, 2012

Added IE compatibility (within the 140bytes size limit):

function(a,i,n){for(n in''+a!==a?a:0)this.style[n]=a[n];return n||i?((this.style[a]=i),this):(this.currentStyle||getComputedStyle(this))[a]}

@williammalo
Copy link
Author

@atk
W00T!
I put it as an alternate.

@atk
Copy link

atk commented May 29, 2012

An annotated version would probably be helpful...

Update: even 1 byte shorter:

function(a,i,n){for(n in''+a!==a&&a)this.style[n]=a[n];return n||i?((this.style[a]=i),this):(this.currentStyle||getComputedStyle(this))[a]}

@williammalo
Copy link
Author

@atk
Cool!

@maettig
Copy link

maettig commented May 29, 2012

Save 2 bytes by turning ((this.style[a]=i),this) into (this.style[a]=i,this). See operator precedence.

@williammalo
Copy link
Author

@maettig
I'm afraid it was already done before you commented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment