0

I'm developing a graph and I need to change the attribute of a div tag. I don't have access to the whole code, so I'm stuck with using JS, no PHP (I guess I don't need that anyway).

I have the following div:

<div data-serie-data="[4.0,4.0,3.0,4.0,3.0]" id="chartContainer">

I want to replace the data-serie-data with what I retrieve from the following JS function:

$.getJSON('*url*', function(data) {
    document.write(data.data);
});

The result from this is:

[5.0,2.0,6.0,1.0,4.0]

How do I replace the current data with this result?

I've tried doing it like this:

$.getJSON('*url*', function(data) {
    document.getElementById("chartContainer").setAttribute("data-serie-data", data.data);
});

But this doesn't change the value. I've also tried setting other attributes but that does nothing aswell.

What am I doing wrong?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
dansan
  • 194
  • 2
  • 2
  • 15
  • Have you check if the server is answering? – Nadir Feb 25 '16 at 12:32
  • Seems all answers are revolving around setting the exact same attribute as you are. What you need to do is to set the attribute and use `setData` to redraw the graph assuming this is highcharts – mplungjan Feb 25 '16 at 12:36
  • Yes, this is highcharts. Looking at the source the attribute isn't even set. I'm not sure what's wrong with it. EDIT: it's actually Highstock, if that makes a difference. – dansan Feb 25 '16 at 12:40
  • It does not matter: http://api.highcharts.com/highstock#Series.setData – mplungjan Feb 25 '16 at 13:18
  • I'm not sure if I can use that, as I dont have access to the graph itself. I can only append new JS to the end of the file. – dansan Feb 25 '16 at 13:34
  • I am pretty sure you can find where it says `new Highcharts.Chart("chartContainer")` or similar and get the chart var from it. – mplungjan Feb 25 '16 at 13:42
  • There's a chart var, but writing the following didn't work: highstockchart.series[0].setData(data.data,true); I posted a question here: http://stackoverflow.com/questions/35628369/update-data-of-existing-highstock-graph-u If you have time to look through this I would greatly appreciate it. – dansan Feb 25 '16 at 13:53

2 Answers2

-1

use the following

 $.getJSON('*url*', function(data) {
     $('#chartContainer').attr("data-series-data", data.data);
 });

if data.data does not work just use data

MasterT
  • 623
  • 1
  • 9
  • 23
  • but it works, and answers the OP original question, as the chart API was not mentioned, originally. hens why we all answered the same. – MasterT Feb 25 '16 at 13:37
  • No it does not change a thing. The question already HAD set the data attribute using DOM. Nothing changes by setting it with jQUery You need to use setData to redraw the chart – mplungjan Feb 25 '16 at 13:38
  • indeed now knowing the graph API the OP used, but without knowing the API, these answers all do what was asked. – MasterT Feb 25 '16 at 13:44
  • They DON'T change what OP already did! You are not answering the actual question just giving a jQuery alternative to what was already tried and discarded as not having any effect! – mplungjan Feb 25 '16 at 14:05
-1

You need to use the jQuery.attr() method.

Try this:

$.getJSON('*url*', function(data) {
    $('#chartContainer').attr('data-serie-data', data.data)
});

Here's a jsfiddle demo.