Code Monkey home page Code Monkey logo

jquery-custom-data-attributes's Introduction

HTML5 custom data attributes jQuery plugin

This plugin simplifies the use of HTML5 custom data attributes in your jQuery code. It adds a function named .dataAttr() to the jQuery namespace, which acts as a wrapper for jQuery.attr().

Example Usage

HTML

<p id="foo" data-time="9 AM"></p>

jQuery

// You can perfectly use this:
$('#foo').attr('data-time'); // '9 AM'
// But I prefer to do it this way, especially when dealing with a lot of data-*
$('#foo').dataAttr('time');  // '9 AM'
// Just like $.attr(), $.dataAttr() can be used as a setter
$('#foo').dataAttr('time', '3 PM');
// Computed data-* attribute values are possible as well
$('p').dataAttr('id', function() {
  return this.id;
}); // p data-id="foo"

Useful?

That’s up to you. This plugin isn’t rocket science.

However, it does allow you to write even more readable code. Personally, I like to keep custom data attributes separated from other attributes in my code — hence this plugin.

Since .dataAttr('foo') minifies to .x('foo'), while .attr('data-foo') minifies to .y('data-foo'), using this plugin can save you some bytes after minification, especially when dealing with a lot of data attributes in your code.

Note: As of jQuery 1.4.3, .data() maps to custom data-* attributes by default, rendering this plugin redundant. It can still be used for older versions of jQuery though.

Author

twitter/mathias
Mathias Bynens

License

This plugin is dual licensed under the MIT and GPL licenses, just like jQuery itself.

jquery-custom-data-attributes's People

Contributors

mathiasbynens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jquery-custom-data-attributes's Issues

jQuery .data() does not render this plugin redundant!

In the README.md you state that jQuerys .data() method renders your plugin redundant. This is not the case as .data() will not access the data-attribute. Instead, jQuery has an internal data store that holds the "data" assigned to each DOM element.

For convenience jQuery will initially fill that data store with the values of the data-attributes when you first select it using jQuery.

That means, that you can't change the value of the data-attribute using .data(). You also will not receive any changes to the data attributes value using .data().

Example:

<div id="test" data-hello="world"></div>
var $test = $('#test');

// You can get the value of the data attribute using .data() after the element has
// been selected for the first time:
$test.data('hello');      //=> 'world'
$test.attr('data-hello'); //=> 'world'

Now lets change the data using .data():

// When you change the data using .data(), the attribute value won't change:
$test.data('hello', 'data');
$test.data('hello');      //=> 'data'
$test.attr('data-hello'); //=> 'world'
<div id="test" data-hello="world"></div>

As you can see, the attribute value has not changed. Now lets use .attr() to update the value of our data-attribute:

// Vice versa, changing the attribute value will not update the data value:
$test.attr('data-hello', 'attribute');
$test.data('hello');      //=> 'data'
$test.attr('data-hello'): //=> 'attribute'
<div id="test" data-hello="attribute"></div>

Now the attribute value has changed but .data() still returns 'data' rather than 'attribute'.

This is essential when you want to use the data-attribute with CSS:

<div id="my-elem" data-size="small">
    <!-- ... -->
</div>
[data-size="small"] {
    /* ... */
}

[data-size="large"] {
    /* ... */
}
$('#my-button').on('click', function() {
    var $myElem = $('#my-elem');

    $myElem.data('size', 'large');      // This will have no effect.
    $myElem.attr('data-size', 'large'): // Yep, that works.
    $myElem.dataAttr('size', 'large');  // Like a charm!
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.