Vlad Stamina Shumiev Vlad Stamina Shumiev - 4 months ago 16
jQuery Question

html yahoo stock not working in two DIV's

Hi stack overflow masters.

I have found a script that allow me to get from yahoo, stock info.
but when i use it in different DIV's only one of them work,
I have tried to fix that (not a programmer =( ), and i dont seem to know what is the problem, and not really know how to find the solution.

code found yahoo stock for html + i have added a checker after div load run function :

<div class="table-box">
<div class="table-detail">
<img width="159px" src="http://patrickcoombe.com/wp-content/uploads/2015/09/new-google-logo-2015.png" />
</div>
<div class="table-detail">
<p class="text-muted m-b-0 m-t-0">GOOGLE</p>
<h4 class="m-t-0 m-b-5"><b><div id='GOOGstock'>$</div></b></h4>
<script type="text/javascript">
function GOOGstock() {
var url = "http://query.yahooapis.com/v1/public/yql";
var symbol = "GOOG";
var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')");

$.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env")
.done(function (data) {
$("#GOOGstock").text("Stock Price: " + "$" + data.query.results.quote.LastTradePriceOnly);
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
$("#GOOGstock").text('Request failed: ' + err);
});
}

var int=setInterval('check()', 500);
function check()
{
if (chkObject('GOOGstock')==true)
{
GOOGstock();
}
}

function chkObject(elemId)
{
return (document.getElementById(elemId))? true : false;
}
</script>
</div>
</div>


Individually it workes good, but when i add another div (duplicate this script as is to other table and change GOOG to INTC stock to show 2 different stock bid prices),
Then it works only for the one that comes last in the html code.


Screenshot - screenshot of the two DIV's created for yahoo stock


how can i make it work in 1 page in different places for different stocks.
I want to make "INTC" & "GOOG" stock to show the bid price only.

thank you all in advance.

Answer

From what you are telling us, it seems like you haven't replaced GOOG everywhere it needs to. Like

<div id='GOOGstock'>$</div></b></h4>

If I change this to 'INTCstock, I will need to change

if (chkObject('INTCstock')==true)
               {
                INTCstock();
               }

And

$("#INTCstock").text("Stock Price: " + "$" + data.query.results.quote.LastTradePriceOnly);

$("#INTCstock").text('Request failed: ' + err);

Here is a working fiddle https://jsfiddle.net/BoyWithSilverWings/5a5r20L2/