Thirumani guhan
Thirumani guhan

Reputation: 398

(this) usage in jQuery

I have the HTML element like this below

<div class="myclassname" data-barcolor="#000000"></div>

And I am trying to get these "data-barcolor" property using (this). keyword, instead of using classname..like below

$('.myclassname').easyPieChart({
    barColor : $(this).attr('data-barcolor'),
});

It doesn't work properly. But

$('.myclassname').easyPieChart({
    barColor : $('.myclassname.').attr('data-barcolor'),
});

Works perfectly.

By Using classname i can't get the values dynamically. I have more than one class in the same name with different values. so how to get the values in jquery!

Upvotes: 1

Views: 120

Answers (1)

dfsq
dfsq

Reputation: 193251

You can use each loop for this:

$('.myclassname').each(function() {
    $(this).easyPieChart({
        barColor : $(this).data('barcolor'),
    });
});

Documentation also states that barColor can be a function, however unfortunately this function is invoked in wrong context (as I can assume from reading sources) so you can't go with natural barColor: function() { return $(this).attr(...); } way.

Upvotes: 6

Related Questions