I am trying to attach a function into an existing button. The button is currently using the jQuery toggle function as of now. What I want to do is the following: When the button is active (user clicks on the button once), show the graph and when the button is inactive (user clicks on the button again), the graph should disappear.
The code I have written is the following (js fiddle is here http://jsfiddle.net/w5h6rffo/7/
HTML
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<button type="button" class="btn" onclick="graphX();">Details</button>
<div id="container" style="height: 400px; min-width: 310px"></div>
CSS
.btn.active, .btn:active {
background: #000000;
text-decoration: none;
}
JavaScript / jQuery
$('.btn').click(function() {
$(this).toggleClass('active');
});
$(function graphX() {
var data = [];
for(var i = 0; i < 899; i++) {
data[i] = {X: i};
}
var processedData = [];
Highcharts.each(data, function (d) {
processedData.push(Math.sin(d.X));
});
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
series: [{
data: processedData,
pointStart: Date.UTC(2010, 1, 1),
}],
});
});
How can I implement the if statement within the jQuery button toggle function so that the graphX is initiated and displayed when the button is active and the graphX to disappear when the button is inactive?
I've been trying to write it in this way
$('.btn').click(function() {
if (.btn.active = false){
container.visibility = visible;
$(this).toggleClass('active');
} else {
container.visibility = hidden;
});
Here is the jsfiddle http://jsfiddle.net/w5h6rffo/7/
if($('#element').hasClass('active')){ /* show graph */ }else{ /* hide it */ }