JSFiddle - React, Tailwind, and code Playground
by Kenneth Luplau-Brøgger
HTML
<input type="text" id="shortname" placeholder="Indtast aktie shortname" />
<textarea disabled></textarea>
JavaScript
$(function() {
$("#shortname").keyup(function() {
var shortname = $(this).val();
getStocksByShortname(shortname, renderStocks);
});
});
var renderStocks = function(stockdata) {
$("textarea").val(JSON.stringify(stockdata));
};
var getShortnameByName = function(longname, callback) {
$.ajax({
type: "GET",
url: "https://query.yahooapis.com/v1/public/yql",
data: {
format: "json",
diagnostics: true,
env: "store://datatables.org/alltableswithkeys",
q: 'select * from yahoo.finance.quote where name in ("' + longname + '")'
},
dataType: "jsonp",
success: function(response) {
if (response.query.results.quote) {
callback(response.query.results.quote);
} else {
$("textarea").val("");
}
}
});
}
var getStocksByShortname = function(shortname, callback) {
$.ajax({
type: "GET",
url: "https://query.yahooapis.com/v1/public/yql",
data: {
format: "json",
diagnostics: true,
env: "store://datatables.org/alltableswithkeys",
q: 'select * from yahoo.finance.quote where symbol in ("' + shortname + '")'
},
dataType: "jsonp",
success: function(response) {
if (response.query.results.quote) {
callback(response.query.results.quote);
} else {
$("textarea").val("");
}
}
});
}