dopaminadarktheme
Full Dopamina Dark Theme
rel and the class attributes on the DOM were quite extensive for DOM selection and arbitary data storage on the browser.
For example, If an list item needs to indicate the user who had 7 notification messages, we sometimes used the class attributes to store the data for the user as mentioned below. This is how some practiced and others had different other ways.
<li class="item user_john message_notify_7"></li>
With the invent of HTML 5, the HTML and Javascript programming has become so flexible, easy and developer friendly and thanks to it, we can not store artibitary data using the data attributes that it provides.
<li class="item" data-user="john" data-message-notify="7"></li>
Isn't this cleaner that what we did previously. The specification for custom data attributes states that any attribute that starts with “data-” will be treated as a storage area for private data. This allows us to maintain cleaner HTML code and also store some extra information that doesn't have any visual representation.
id attributes so that it is easy to understand the initial example.
<li id='itemJohn' class="item" data-user="john" data-message-notify="7"></li>
var item = document.getElementById('itemJohn');
var custom_attributes = item.dataset;
//-> data-user="john"
//-> data-message-notify="7"
var custom_attributes = $('#item').data();
//returns -> { user:john, message-notify:7}
The data() additionally take 2 optional parameters that helps in specific selection of the attributes and the store values.
var user_notifications = $('#userJohn').data('message-notify');
//returns -> 7
$('#userJohn').data('message-notify',10);
The above example will update the data attributes message-notify from 7 to 10.
If a particular data attribute is not present when setting the value, a new one is automatically created and added to the dataset of the DOM Element.
<ul>
<li data-user="john" data-friends="4" data-admin="YES"></li>
<li data-user="doe" data-friends="5" data-admin="NO"></li>
<li data-user="jessy" data-friends="6" data-admin="YES"></li>
<li data-user="michael" data-friends="7" data-admin="NO"></li>
<li data-user="jane" data-friends="1" data-admin="YES"></li>
<li data-user="george" data-friends="14" data-admin="NO"></li>
</ul>
The jquery selector can be used to get all the objects that use the custom data attribute data-user. To do this, we use:
var users = $('[data-user]');
This will return a list of li objects that have the custom attribute data-user. You can now access all the users by looping through the users variable.
$.each(users,function(idx,item){
var isAdmin = $(item).data('admin');
var friends_count = $(item).data('friends');
$(item).data('notifications','4');
});
If you wanted a list of all the users who are admin, then:
var admin_users = $('[data-admin="YES"]');
This will return the li objects with data-user belonging to JOHN|JESSY|JANE
You can create a helper method to search dynamically
function fetchByAdminType($type){
return $('[data-admin="' + $type + '")]');
}
var admin_users = fetchByAdminType('YES');
var non_admin_users = fetchByAdminType('NO');
li[data-admin="YES"] {
color: '#bbb';
background: '#555';
font-weight: 'bold';
}
Christian Heilmann shows more ways on how to use data attributes in this screencast.
The datalist API is supported by all modern browsers but not IE10 and below. A shim is available but it’s possibly more practical to use jQuery if you’re coding for the older browsers. Assine a newsletter para receber em seu email as publicações atualizadas neste blog