2

Many jQuery plugins have an options array which allows you to specify certain settings.

Is it possible to write a function directly into the plugins options array to add a value from an HTML data-attribute? or pull any HTML content for that matter?

Example:

The following is for the Circliful plugin which creates a simple circular chart based on the percentage value you specify in percent: which obviously wants an integer.. but what I'd like to do is pull a value from the DOM and add it as the percent value.

I've wanted to do this on a number of occasions but I've always ended up finding workarounds, it'd be great to know if it's possible.

$(document).ready(function() {
    $("#your-circle").circliful({
        animationStep: 5,
        foregroundBorderWidth: 5,
        backgroundBorderWidth: 15,
        percent: 50 //get value of a data attribute and put it here
    });
});

3 Answers 3

2

from the plugin documentation you seem to do this by default using data-percent attribute

see:https://github.com/pguso/jquery-plugin-circliful#data-attributes

or you can create a global variable and pass the value to the property:

$( document ).ready(function() {
        var percent = $("#your-circle").attr('data-percent');
        $("#your-circle").circliful({
        animationStep: 5,
        foregroundBorderWidth: 5,
        backgroundBorderWidth: 15,
        percent: percent //get value of a data attribute and put it here
});
});

(note: seems that the data-attributes only work with a earlier version of the plugin )

Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly! I can't believe it's as simple as adding a var. I was seriously over-complicating it in my head. Thanks very much Madalin.
1

You can always read element data from your DOM. It's type is string, so some parsing may be needed tho. You might need to save it somehow beforehand, but if you load your scripts in $( document ).ready(function( //scripts )) it should work fine.

<div id='element' data-abc='def'></div>


percent: $('#element').data('abc')

3 Comments

This works as well and it's such a simple, clean approach. Madalin submitted a correct answer quicker so I accepted it.. not familiar with S.O. etiquette when it comes to marking or voting answers?
Thanks, I am not familiar as well and it doesn't really matter as anyone who is looking for an answer will get it here anyway :)
Great attitude! We need more selfless people in the world. Thanks again for your help.
0

Have you tried:

<div id="your-circle" data-percent="50"></div>

$("#your-circle").circliful({
    animationStep: 5,
    foregroundBorderWidth: 5,
    backgroundBorderWidth: 15,
    percent: parseInt($(this).attr('data-percent'),10)
});

?

1 Comment

Interesting approach. I couldn't get it to work, the percentage counter just keeps counting up indefinitely :-S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.