2

Basically what i am trying do is retrieving stock quotes for specific company . In my code I am giving symbol of specific company(eg:FB) in a textbox(symb) and when i click the button(getupdate) it should list out details regarding that specific stock which i enter the text box .

Here's my code :

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function($){
$('getupdate').click(function() {

        var symbol = $('input[id=symb]').val(); \\For Example:FB

        var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'+symbol+'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

        $.getJSON(url, function(data) {
            var items = [];
            $.each(data.query.results.quote, function(key, val) {
                items.push('<li id="' + key + '">' + val + '</li>');

            });
            $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('body');
        });
});
});
</script>

</head>
<body>
<div style="padding:16px;">
    Stock Ticker : <input id="symb" type="textbox" value="Ticker"></input>
</div>
<button id="getupdate" name = "getupdate" type="button">Get Updates!</button>
</body>
</html>

Where am i going wrong ?

Thanks in advance.

Deepesh Shetty
  • 1,116
  • 3
  • 18
  • 43

4 Answers4

2

You have several mistakes in your JS code:

  1. Incorrect selector for a button: $('getupdate') => $('#getupdate');
  2. Wrong quotes inside url value;
  3. Wrong query string to Yahoo API;
  4. Wrong comment sign \\For Example:FB.

Your JS should be like this:

jQuery(document).ready(function($){
    $('#getupdate').click(function() {
            var symbol = $('input[id=symb]').val(); 
            var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22' + symbol + '%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
            $.getJSON(url, function(data) {
                var items = [];
                $('#results').html('');
                $.each(data.query.results.quote, function(key, val) {
                    items.push('<li id="' + key + '">' + val + '</li>');

                });
                $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('#results');
            });
    });
});

And please add this code after <button> tag in your HTML. This will help you easily clear results before new query:

<div id="results"></div>

Here is an example: http://jsfiddle.net/6EFqk/1/

I'm not shure that ouput format is correct, please reformat it as you like.

Mikhail Chernykh
  • 2,659
  • 22
  • 15
  • 1
    Thank you . Its working Fine now . But the problem is when i change the text box value and click the button again it wont show me quotes of new value which i entered in text box (For example : I give the value FB in text box and click get updates it will show me quotes of FB , but when i change the value from FB to say IBM and then click the button it will show me previous stocks values of FB . Now i have to refresh every time i enter a new value to the text box . Any help ? – Deepesh Shetty Apr 18 '13 at 15:01
  • 1
    As, you are using `appendTo` function, it add the new content to the end. I've added new `div` and now I'm replace it's content every time. I've modified an example: http://jsfiddle.net/6EFqk/1/ – Mikhail Chernykh Apr 18 '13 at 15:03
1

First, you aren't selecting the button correctly.

$('#getupdate').click(function() {

Secondly, your slashes are backwards for a comment, they are always forward not back.

//For Example:FB

And finally, you aren't getting any results for this reason given back by the service:

The current table 'yahoo.finance.quotes' has been blocked. It exceeded the allotted quotas of either time or instructions

http://jsfiddle.net/RVFW3/

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 1
    Thank you for the quick response . I made the necessary changes . But why is that table blocked ? I don't get the statement "It exceeded the allotted quotas of either time or instructions " . – Deepesh Shetty Apr 18 '13 at 14:45
  • I don't know, i've never used that service. – Kevin B Apr 18 '13 at 14:47
  • 1
    @Keven B Its working Fine now . But the problem is when i change the text box value and click the button again it wont show me quotes of new value which i entered in text box (For example : I give the value **FB** in text box and click get updates it will show me quotes of **FB** , but when i change the value from **FB** to say **IBM** and then click the button it will show me previous stocks values of **FB** . Now i have to refresh every time i enter a new value to the text box . Any help ? – Deepesh Shetty Apr 18 '13 at 15:00
  • `jQuery(document).ready(function($){ $('#getupdate').click(function() { var symbol = $('input[id=symb]').val(); var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'"+symbol+"'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";` – Deepesh Shetty Apr 18 '13 at 15:04
  • 1
    It worked fine for me . The above answer . Thanks a lot mate for the help . – Deepesh Shetty Apr 18 '13 at 15:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28491/discussion-between-deepesh-shetty-and-kevin-b) – Deepesh Shetty Apr 19 '13 at 06:53
0

I just found this stream doing a google search on the subject.

Perhaps this site will answer your questions ... http://bitbenderz.com/stockticker/

  • 1
    Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Oct 05 '13 at 12:26
0

Here is a version of the solution: jsfiddle

$.getJSON(yqlUrl1, function(data){
    $.each(data.query.results.row, function(index, item){

    var element = $('<div></div>');

    element.append('<span>' + item.symbol + '</span> ');
    element.append('<span>' + item.price + '</span> ');

    if (item.change.indexOf('+') > -1) {
        element.append('<span class="stockUp"> ' + item.change + '</span>');
    } else {
        element.append('<span class="stockDown"> ' + item.change + '</span>');
    }

    element.appendTo('#quotes');   
    });
})
BENARD Patrick
  • 30,363
  • 16
  • 99
  • 105
Marc
  • 1