I need your help to set cookies in a select.
I just need save the value of this select for the next time load the page, the value will be the same that last time selected. Really I dont have so much experience with javascript/ajax for that reason my post. I hope you guys can help me. Please find bellow my short code:
value to save as cookies (data-url)
<select id="timePeriod">
<option data-url="https://api.myjson.com/bins/12at1e">Minute</option>
<option data-url="https://api.myjson.com/bins/16cjya">Hour</option>
<option data-url="https://api.myjson.com/bins/1fecci">Day</option>
<option data-url="https://api.myjson.com/bins/1fecci">Forever</option>
</select>
function drawHighchart(timeFramUrl) {
$.ajax({url: timeFramUrl, success: function(data){
var currentValue = (data[data.length - 1][1]);
Highcharts.chart('container', {
chart: {
zoomType: 'x',
events: {
load: function() {
var series = this.series[0];
var chart = this;
var yAxis = chart.yAxis[0],
plotLine,
d,
newY;
yAxis.addPlotLine({
value: currentValue,
color: '#ff5959',
width: 2,
zIndex: 5,
id: 'plot-line-1',
label: {
text: currentValue,
style: {
color: '#ff5959',
fontWeight: 'bold'
}
}
});
reload = setInterval(function() {
$.getJSON(timeFramUrl + '?v=' + Math.random(), function(recData) {
var currentValue = (recData[recData.length - 1][1]);
var currenttime = (recData[recData.length - 1][0]);
series.setData(recData);
var x = currenttime,
y = currentValue;
series.addPoint([x, y], true, true);
plotLine = yAxis.plotLinesAndBands[0].svgElem;
d = plotLine.d.split(' ');
newY = yAxis.toPixels(y) - d[2];
plotlabel = yAxis.plotLinesAndBands[0].label;
plotlabel.animate({
translateY: newY,
text: Highcharts.numberFormat(y, 2)
}, {
duration: 400,
step: function() {
$(this.element).html(y);
},
complete: function() { }
}),
plotLine.animate({
translateY: newY
}, 400);
});
}, 5000);
}
}
},
title: { text: ''},
exporting: { enabled: false },
credits: { enabled: false },
xAxis: { type: 'datetime'},
yAxis: { opposite: true,
labels: {
align: 'left',
},
gridLineWidth: 0,
title: {
text: 'Exchange rate'
},
},
plotOptions: {
areaspline: {
marker: {
enabled: false
}
}
},
series: [{
name: 'Exchange BTC to EUR',
data: data,
type: 'areaspline',
threshold: null,
animation: true,
tooltip: {
valueDecimals: 2
},
fillColor: {
linearGradient: { x1: 5, y1: 0, x2: 0, y2: 0
},
stops: [ [0, Highcharts.getOptions().colors[0]], [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')] ]
}
}]
});
}});
}
// To save as cookie
$(document).ready(function() {
var srcURL = $(':selected', '#timePeriod').data('url');
drawHighchart(srcURL);
$('#timePeriod').change(function(){
var srcURL = $(':selected', this).data('url');
drawHighchart(srcURL);
});
});
$('#timePeriod').change(function(){
clearInterval(reload);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<!-- To save as cookies -->
<select id="timePeriod">
<option data-url="https://api.myjson.com/bins/12at1e">Minute</option>
<option data-url="https://api.myjson.com/bins/16cjya">Hour</option>
<option data-url="https://api.myjson.com/bins/1fecci">Day</option>
<option data-url="https://api.myjson.com/bins/1fecci">Forever</option>
</select>
<div id="container" style="height: 400px; width: 100%"></div>