'Count if an element has a certain class and it's data attr contains a number
Trying to get a count of each label element that has the class "active" and it's input attr, "data-price", contains just a number/price between the [""].
So, if tot element count is > 0, do something...
HTML
<ul class="elem-ul">
<li class="elem-li">
<label class="elem-label active">
<input class="elem-input" data-price="["100"]" type="checkbox">
</label>
</li>
<li class="elem-li">
<label class="elem-label active">
<input class="elem-input" data-price="["400"]" type="checkbox">
</label>
</li>
<li class="elem-li">
<label class="elem-label active">
<input class="elem-input" data-price="[""]" type="checkbox">
</label>
</li>
<li class="elem-li">
<label class="elem-label">
<input class="elem-input" data-price="[""]" type="checkbox">
</label>
</li>
</ul>
jQuery I am not sure how to get the number from the input attr
$('.elem-ul .elem-label.active').each(function(){
var elemCt = $('input.elem-input', this).attr('data-price').length;
if(elemCt > 0){
//do something
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
