I have read multiple different articles and questions to no avail. I have a PHP page that will use AJAX to update an easypiechart using values from a database with a check every X minutes. In this example I have reduced the time to 10 seconds with an alert box to show what changes have been made.
I can use the attr() to view the "data-percent" however it returns "Object Object" if I attempt to change the value of the "data-percent" attribute.
I am trying to manually just change the value to 90 for the time being until I can get this function to work before adding the ajax response as the replacement.
code:
<!-- Link to Google CDN's jQuery + jQueryUI; fall back to local -->
<div id="test" class="easy-pie-chart txt-color-blue easyPieChart" data-percent="38" data-pie-size="160">
<span class="percent percent-sign txt-color-blue font-lg semi-bold"></span>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
if (!window.jQuery) {
document.write('<script src="js/libs/jquery-2.1.1.min.js"><\/script>');
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script>
if (!window.jQuery.ui) {
document.write('<script src="js/libs/jquery-ui-1.10.3.min.js"><\/script>');
}
</script>
<script>
// Setup AJAX request -->
function updateWidgets(){
$.ajax({
url: "widgetGet.php",
type: "POST",
success: function(data) {
var response = $.parseJSON(data);
var idea = parseInt(response.test);
var ID = $("#test").attr("data-percent");
var IDchange = $("#test").attr("data-percent", 90);
alert(ID);
}
});
}
updateWidgets();
setInterval(updateWidgets, 10000);
</script>
If you switch the alert from ID to IDchange you can see where my problem is. Thanks guys as I know I am in the hands of a great community here.
Update, understandably the alert function shouldn't be used for debugging. What the issue (and not sure if attr()) is the solution is to update the html attribute using a AJAX response that is initiated every 10 seconds. Documentation such as this show that attributes are updated using the attr() which leads to my confusion.
In short, how can I update the html attribute of the div containing the easypiechart using AJAX that is receiving data every 10 seconds?
alert()for debugging - it coerces everything to a string and it blocks the UI thread. Always useconsole.logorconsole.dir.data()method is, in my opinion, preferable to fooling with the attributes directly, though it's not exactly the same as doing so.data()is still much faster than accessing the DOM. Sadly jsperf.com is no longer with us so I can't look it up quickly.