I am interested in adding classes or adding attributes to elements to dynamically style them.
An agreement to apply CSS properties to specific elements that satisfy certain parameters is usually done by applying a class to that element. For example, if I click on a button, we can say that this button is in an active state - I could choose to apply a special class to it, for example:
$(".button").click(function(){ $(this).addClass("active"); });
CSS will be as simple as:
.button.active { transform: scale(1.5); }
My approach is different, and I'm curious if there is any noticeable difference between the two. I apply attributes to elements instead of classes, for example:
$(".button").click(function(){ $(this).attr("active", true); });
With CSS as follows:
.button[active] { transform: scale(1.5); }
I am wondering if there are any reasons why I should not do this, if this is a bad agreement or something else, and if there is any performance difference in this method. Mostly just curious, but also interesting, for example, if you use queries like $(".button[active]") , are less effective than $(".button .active") .
source share