Change Data-attribute On Click Of Html Elements
I am trying to apply changes to the DOM when a is clicked. When clicked, it should add data-state=enabled and data-display=expanded to the clicked while apply
Solution 1:
Here is a pure javascript example in the below jsfiddle:
http://jsfiddle.net/pya9jzxm/14
var tbody = document.querySelector('tbody');
var trs = tbody.querySelectorAll('tr');
var tr, index = 0, length = trs.length;
for (; index < length; index++) {
tr = trs[index];
tr.setAttribute('data-state', 'enabled');
tr.setAttribute('data-display', 'collapsed');
tr.addEventListener('click',
function () {
if (this.classList.contains('alphabet-label')) {
return;
}
var trIndex = 0, trLength = trs.length, hasExpanded = false;
var state = 'disabled';
if (tbody.querySelectorAll('[data-display="expanded"]').length > 0) {
hasExpanded = true;
state = 'enabled';
}
for (; trIndex < trLength; trIndex++) {
trs[trIndex].setAttribute('data-state', state);
trs[trIndex].setAttribute('data-display', 'collapsed');
}
if (!hasExpanded) {
this.setAttribute('data-state', 'enabled');
this.setAttribute('data-display', 'expanded');
}
}
);
}
};
Solution 2:
in jQuery:
$(function() {
$('html').click(function() {
/* return to default state */
$('.table tbody tr').data('state', 'enabled').data('display', 'expanded');
});
$('.table tbody tr').on('click', function(e) {
/* stop the html click event */
e.stopPropagation();
/* set attributes for all rows */
$('.table tbody tr').data('state', 'disabled').data('display', 'collapsed');
/* set attributes for the clicked row */
$(this).data('state', 'enabled').data('display', 'expanded');
});
});
I've commented the function, to clarify how it works.
Solution 3:
Which is very simular to @LinkinTED
Please inspect the html before you say this does not work.. I've tested it myself.
May i suggest that you instead modify the class... to change the effects.
$(function() {
var $row = $('.table tbody tr');
$('html').click(function() {
$row.attr('state', 'enabled').attr('display', 'expanded');
});
$row.on('click', function(e) {
e.stopPropagation();
$row
.attr('state', 'disabled')
.attr('display', 'collapsed');
//console.log(e);
$(this)
.attr('state', 'enabled')
.attr('display', 'expanded');
});
});
Solution 4:
Using vanilla JS and event delegation. DEMO
var tablerow = document.body.getElementsByTagName('tr');
// Convert the HTMLCollection into a true javascript Array, so we can do "stuff" with it var tablerowArr = Array.prototype.slice.call(tablerow);
// store the highlighted rowvar highlighted;
// extracted function to enable/disable rowsvar enable = function(row){
row.setAttribute('data-display', "expanded");
row.setAttribute('data-state', "enabled");
}
var disable = function( row ){
row.setAttribute('data-display', "collapsed");
row.setAttribute('data-state', "disabled");
}
// on click anywheredocument.body.addEventListener('click', function(e){
var target = e.target;
// check if event target or any of its parents is a TRwhile( target.parentNode && target.nodeName.toLowerCase() != 'tr' ){
target = target.parentNode;
}
// if s row was higlighted disable all rowsif( highlighted ){
tablerowArr.forEach( disable );
// and remove the reference to highlighted
highlighted = null;
}
// no row is highlightedelse {
tablerowArr.forEach( function(row){
// if the event target is a row, highlight itif(row == target) {
enable( row );
// and store it as the currently highlighted
highlighted = row;
}
// if it's not the event target then disableelse {
disable( row );
}
});
}
});
Post a Comment for "Change Data-attribute On Click Of Html Elements"