0

i have a javascript widget that will display a data chart.. and i have an dropdown box which gives the user the ability to change the theme. so when the user selects a theme it should change and show the user the new theme.

Widget code:

<script type="text/javascript">
new TradingView.widget({
  "width": 500,
  "height": 400,
  "interval": "1",
  "timezone": "Etc/UTC",
  "theme": "White",
  "style": "2",

}); 
</script>

HTML/JS code

<select>
    <option value="moonlight">Moon</option>
    <option value="darkness">Dark</option>
</select>

<script>
$(document).on('change','.theme',function(){
      alert(this.value);
    });
</script>

How do i get add the this.value to the widget JS code theme parameter?

2
  • Are you using any js plugin? Commented Dec 7, 2015 at 13:20
  • @deepakb Looks like jQuery and is tagged with jQuery. Commented Dec 7, 2015 at 13:27

2 Answers 2

3

I guess you should put the new widget code in a function and access the theme from the args:

<script type="text/javascript">
function themeChanger(theme){
  var themeWidget = new TradingView.widget({
    "width": 500,
    "height": 400,
    "interval": "1",
    "timezone": "Etc/UTC",
    "theme": theme,
    "style": "2",

  }); 
  return themeWidget;
}

$(document).on('change','.theme',function(){
   var widget = themeChanger(this.value);
   // do something with widget.
});

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

just got one small question..i added the same to different widget but when i choose an option from the dropdown, the widget loads like in a blank page and the dropdown is not visible. jsfiddle.net/livewirerules/3e9jaLku/5 can u please have a look at that fiddle and tell me what am i doing wrong there?
It seems it is replacing everything in the body with the widget.
0

you can use global variable

var chartParams={
  "width": 500,
  "height": 400,
  "interval": "1",
  "timezone": "Etc/UTC",
  "theme": "White",
  "style": "2",

}
new TradingView.widget(chartParams);
$(document).on('change','.theme',function(){
     chartParams["theme"]=this.value
     new TradingView.widget(chartParams);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.