Reputation: 1
I want to set the specified label text to span width Here is the code. Thanks for your help.
function setBarWidth(labelText, barElement, cssProperty, barPercent) {
var listData = [];
$(labelText).each(function () {
listData.push($(this).textContent);
});
var listMax = Math.max.apply(Math, listData);
$(barElement).each(function(index) {
$(this).css(cssProperty, (listData[index] / listMax) * barPercent + "%");
});
}
setBarWidth(".style-1 span label", ".style-1 em", "width", 100);
setBarWidth(".style-2 span label", ".style-2 span", "width", 55);
Upvotes: 0
Views: 152
Reputation: 780798
textContent
is a DOM property, but $(this)
is a jQuery object. You should use this.textContent
or $(this).text()
.
You can also replace .each
with .map
var listData = $(labelText).map(function() {
return this.textContent;
}).get();
.get()
at the end is necessary to convert the jQuery collection into an ordinary array.
Upvotes: 1