4

I am trying to change the ticker widget of an embedded tradingview iframe onclick according to the selected symbol. The JS part inside the onclick function looks like this:

document.getElementById("trading-view").innerHTML = ""; 
var script = document.createElement("script"); 
    script.setAttribute('src', 'https://s3.tradingview.com/external-embedding/embed-widget-technical-analysis.js'); 
    script.text = '{' + 
      '"width": "100%",' +
      '"height": "100%",' +
      '"symbol": "AAPL",' +       
      '"locale": "en",' +
      '"interval": "1D"' +
    '}'; 
    document.getElementById("trading-view").appendChild(script);

It basically resets the iframe but uses always the same symbol. I tried to create a variable called symbol and wanted to use it instead of the "AAPL" string but it didn't work. It would be great if you could answer that! (I searched for similar problems of embedding tradingview on stackoverflow but most of the answer where based on Node and didn't help me.)

Philip_F
  • 43
  • 4
  • Can you show what you tried that didn't work? – Frank Modica Dec 10 '18 at 18:35
  • Sure Frank Modica! I did not have any problems getting the entered symbol using var symbolx = document.getElementById("abc"). But I was not able to use the symbolx variable instead of AAPL. I tried it without paragraphs as well as with various ways of using paragraphs. I also tried script.replace below to replace but it neither worked. Would be great to get new ideas what to try next! – Philip_F Dec 10 '18 at 18:42
  • So at that point you had the DOM element in `symbolx`, but not the text itself? – Frank Modica Dec 10 '18 at 18:47
  • I had the required string "AMZN" in symbolx, but was not able to call it in the script.text part of the code above. E.g. by trying '"symbol": symbolx – Philip_F Dec 10 '18 at 18:49
  • Try `'"symbol": "' + symbolX + '",' + ` – Frank Modica Dec 10 '18 at 18:56
  • Thank you so much, that worked perfectly! – Philip_F Dec 11 '18 at 09:03

1 Answers1

3

You have to append the variable to the string (which is surrounded by single quotes) in such a way that the double quotes remain. Try:

'"symbol": "' + symbolX + '",' + 
Frank Modica
  • 10,238
  • 3
  • 23
  • 39