I have a script that loads when the page loads, and loads a chart into a div. The script is placed directly in the div like the docs show to do.
<div id="chart2" style='height:100%;'>
<script type="text/javascript">
var chart2 = new TradingView.widget({
"autosize": true,
"interval": "60",
"timezone": "Etc/UTC",
"locale": "en",
"toolbar_bg": "rgba(255, 255, 255, 1)",
});
</script>
</div>
The chart loads directly into div#chart2. TradingView widget doesn't seem to allow you to programmatically change the symbol only of the chart.
So to load new data from a different symbol, it seems I have to destroy the chart and reload it. Which works fine by doing
$('.chooseSymbol').click(function(){
$('#chart2').html("");
var symbol = $(this).data('symbol');
var sc = '<script>var chart2 = new TradingView.widget({"autosize": true,"symbol": "POLONIEX:'+symbol+'","interval": "60","timezone": "Etc/UTC","theme": "White","style": "1","locale": "en","toolbar_bg": "rgba(255, 255, 255, 1)","enable_publishing": false,"hideideas": true});<\/script>';
$("#chart2").append(sc);
});
However, this doesn't seem to append it to only #chart2. It seems to replace the whole body of the entire page, and makes a full page view of the chart. Symbol changes fine though, but I need it to just replace the current chart, inside the div.
PS I know I can allow the tradingview dropdown, so a user can select their symbol. But I want the user to choose only symbols I allow on the site. Therefore I've hidden the symbol input and chosen this option.