javascript - How to change data attribute value? - Stack Overflow

This might be a simple question but I have been experimenting for the last hour or so without a success

This might be a simple question but I have been experimenting for the last hour or so without a successful conclusion. Basically, I have the following HTML:

<div data-trackcolor="#f39e93" data-size="60" data-linewidth="3" data-animate="500" data-percent="0" data-linecap="butt" data-barcolor="#fff" class="easyPieChart" data-toggle="easypiechart" style="width: 60px; height: 60px; line-height: 60px;">
  <span class="white font-90">0%</span>
  <canvas height="65" width="65" style="width: 60px; height: 60px;"></canvas>
</div>

I'm trying to change the value of data-trackcolor element using the following jQuery syntax:

$('[data-toggle=easypiechart]').data('trackcolor','#f39e93');

I also tried $(".easyPieChart").attr("data-trackcolor", "#f39e93");.

The value is changing, the color is not changing. I'm suspecting this is because of the canvas element. In order to understand what this markup does, you can have a look at the following URL.

It displays the round circle in the colored databoxes. I would appreciate if someone might have some ideas why this is not working.

Thanks.

This might be a simple question but I have been experimenting for the last hour or so without a successful conclusion. Basically, I have the following HTML:

<div data-trackcolor="#f39e93" data-size="60" data-linewidth="3" data-animate="500" data-percent="0" data-linecap="butt" data-barcolor="#fff" class="easyPieChart" data-toggle="easypiechart" style="width: 60px; height: 60px; line-height: 60px;">
  <span class="white font-90">0%</span>
  <canvas height="65" width="65" style="width: 60px; height: 60px;"></canvas>
</div>

I'm trying to change the value of data-trackcolor element using the following jQuery syntax:

$('[data-toggle=easypiechart]').data('trackcolor','#f39e93');

I also tried $(".easyPieChart").attr("data-trackcolor", "#f39e93");.

The value is changing, the color is not changing. I'm suspecting this is because of the canvas element. In order to understand what this markup does, you can have a look at the following URL.

It displays the round circle in the colored databoxes. I would appreciate if someone might have some ideas why this is not working.

Thanks.

Share Improve this question edited May 28, 2017 at 23:26 Badacadabra 8,5357 gold badges31 silver badges54 bronze badges asked Jun 1, 2015 at 11:04 krafokrafo 1835 silver badges20 bronze badges 4
  • The color of what is not changing? and how is the color supposed to change by itself? – painotpi Commented Jun 1, 2015 at 11:19
  • 1 why would color change based on what you put in your data-X attribute? – Praveen Puglia Commented Jun 1, 2015 at 11:21
  • attributes are not properties those changes. You might need to put some extra script to get this done. – Manoz Commented Jun 1, 2015 at 11:24
  • @krafo did my answer helped you ? – Tiberiu C. Commented Aug 14, 2015 at 9:22
Add a ment  | 

3 Answers 3

Reset to default 5

These data values are being used only once at the loading of page as per code written in THIS script file (from the link provided by you). So your changes made in these attributes dynamically won't affect the Charts colors or anything else.

Thanks Puneet,

I was actually calling the script on page load, in order to solve the issue i put the call to the script in the function which changes the colors like the following:-

function changeWidgetColor()
{
    var percentageValue = $(".easyPieChart").attr('data-percent');

    if (percentageValue <= 20)
    {
        $(".easyPieChart").attr("data-trackcolor", "#f39e93");

        InitiateEasyPieChart.init();
    }

}

With $.data you can read and set data values but you can not write in the DOM with it. From jQuery documentation of data method : https://api.jquery./data/

Additional Notes:

Note that this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.

Update

Don't mind this, I've confused the question.

As an alternative use the attr setter :

$('#testBtn').on('click', function (ev){ 
 $("[data-toggle='easypiechart']").attr('data-trackcolor', '#f39e93');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-trackcolor="#fff" data-size="60" data-linewidth="3" data-animate="500" data-percent="0" data-linecap="butt" data-barcolor="#fff" class="easyPieChart" data-toggle="easypiechart" style="width: 60px; height: 60px; line-height: 60px;">
<span class="white font-90">0%</span>
<canvas height="65" width="65" style="width: 60px; height: 60px;"></canvas>
</div>

<button id="testBtn">Test</button>

After some further analyze I concluded : You don't need to change the data attribute because they are read only once at loading. To change the track color you need to get the easyPieChart instance and update it's options and trigger the update function.

The instance is assigned in the element data with the property name easyPieChart.

See : http://beyondadmin-v1.4.s3-website-us-east-1.amazonaws./assets/js/charts/easypiechart/jquery.easypiechart.js at line 167

   ...
   return c.data("easyPieChart", new a.easyPieChart(e, f))
   ...

Putting all together

// simulates the initialisation
InitiateEasyPieChart.init();

// 
var getPies = function(){
 return $('[data-toggle="easypiechart"]');
};

var changePiesTrackColor = function(color){
  var $pies = getPies();
  $pies.each(function(idx, el){
    var $el = $(el);
    var easypiechartInstance = $el.data('easyPieChart');
    easypiechartInstance.options.trackColor = color;
    var currentValue  = $el.data('percent'); // there is no way to get the current Value from the instance because the scop of it is "privat".
    easypiechartInstance.update(currentValue);
    
  });
};
<div data-trackcolor="#f39e93" data-size="60" data-linewidth="3" data-animate="500" data-percent="10" data-linecap="butt" data-barcolor="#fff" class="easyPieChart" data-toggle="easypiechart" style="width: 60px; height: 60px; line-height: 60px;">

</div>

<hr />
<button onclick="changePiesTrackColor(getcolor('#0000ff'))">blue</button>
<button onclick="changePiesTrackColor(getcolor('#00ff00'))">green</button>
<button onclick="changePiesTrackColor(getcolor('#ff0000'))">red</button>

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="http://beyondadmin-v1.4.s3-website-us-east-1.amazonaws./assets/js/beyond.min.js"></script>
<script src="http://beyondadmin-v1.4.s3-website-us-east-1.amazonaws./assets/js/charts/easypiechart/jquery.easypiechart.js"></script>
<script src="http://beyondadmin-v1.4.s3-website-us-east-1.amazonaws./assets/js/charts/easypiechart/easypiechart-init.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745106906a4611614.html

相关推荐

  • javascript - How to change data attribute value? - Stack Overflow

    This might be a simple question but I have been experimenting for the last hour or so without a success

    13小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信