Skip to content

Instantly share code, notes, and snippets.

@cyberstream
Forked from 140bytes/LICENSE.txt
Created February 10, 2012 06:02
Show Gist options
  • Save cyberstream/1787092 to your computer and use it in GitHub Desktop.
Save cyberstream/1787092 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

HTMLElement.prototype.data = function(attr, val) {
/* If only the attribute parameter was passed to the function,
* then get data-{attr}. If data-{attr} is not set,
* then return false
*/
if (attr && typeof val != 'undefined') return this.setAttribute('data-' + attr, val);
/* If both the attribute and value parameters
* were passed to the function,
* then assign {val} to data-{attr} in the current element
*/
else if (attr) return this.getAttribute('data-' + attr) || false;
// if no parameters were passed, then return false
else return false;
}
HTMLElement.prototype.data=function(a,v){return(a&&typeof v!='undefined'?this.setAttribute('data-'+a,v):(a?this.getAttribute('data-'+a)||!1:!1))}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Eli Mitchell <http://www.cyberstream.us>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "getOrSetHTML5DataAttributes",
"description": "Gets or sets a data-attribute in the given HTML element",
"keywords": [
"attribute",
"data",
"HTML5",
"DOM"
]
}
<!DOCTYPE html>
<title>Get/Set Data Attributes</title>
<div id="about_me" data-name="Eli Mitchell">About Me (click to see the address of my website)</div>
<p id="name">loading...</p>
<p id="website"></p>
<script>
HTMLElement.prototype.data=function(a,v){return(a&&typeof v!='undefined'?this.setAttribute('data-'+a,v):(a?this.getAttribute('data-'+a)||false:false))}
window.addEventListener('DOMContentLoaded', function() {
var about_me = document.getElementById('about_me'),
name_elem = document.getElementById('name'),
website_elem = document.getElementById('website');
name_elem.innerHTML = 'Hey, I\'m ' + about_me.data('name');
about_me.onclick = function() {
about_me.data('website', 'http://www.cyberstream.us');
website_elem.innerHTML = 'My website is ' + about_me.data('website');
}
}, false);
</script>
@tsaniel
Copy link

tsaniel commented Feb 11, 2012

Save 2 bytes.

HTMLElement.prototype.data=function(a,v){return!!a&&this[(v!=null?"s":"g")+"etAttribute"]("data-"+a,v)||!1}

@cyberstream
Copy link
Author

@tsaniel nice! It still works fine, but it returns false when assigning a value, e.g. elem.data('country', 'United States'). Should we worry about return values in this case?

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