One method I frequently employ is using classes
to group related elements, such as:
<input value="10" class="sum-this" />
<input value="20" class="sum-this" />
<input value="30" class="sum-this" />
The sum-this
class does not have any associated CSS and is not defined in any CSS files - it's solely utilized in jQuery like this:
var total = 0;
$(".sum-this").each(function(i, el){
total += parseInt($(el).val());
});
console.log(total); // 60?
Is there a more appropriate way to achieve this? Would using another attribute like rel
or data-*
be better?