The Yahoo stock display is experiencing issues within two separate div elements on the webpage

Hey experts on stack overflow!

I came across this script that allows me to fetch stock information from Yahoo. However, I'm facing an issue where the script only works for one div when I try to use it in multiple divs. I'm not a coder, so I'm struggling to identify the problem and find a solution.

Below is the code for fetching Yahoo stock info for Google, along with a checker function after the div loads:

<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>

While this works fine individually, when I try to duplicate it for another stock (replace GOOG with INTC for a different bid price), it only works for the one that is placed last in the HTML code.

Screenshot - Check out the screenshot of the two divs created for Yahoo stock here.

Can you help me make it work on one page in different places for different stocks? I specifically want to display the bid price for "INTC" and "GOOG" stocks.

Thank you all in advance.

Answer №1

It appears from your explanation that there may be instances where you have not properly replaced the placeholder 'GOOG'. For example

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

If 'GOOGstock' is changed to 'INTCstock', the following adjustments will need to be made

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

Also, in the code snippets below:

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

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

You can view a working example on JSFiddle at https://jsfiddle.net/BoyWithSilverWings/5a5r20L2/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble with displaying a custom marker on Google Maps API v3

I have been struggling to replace the default marker with a custom icon in my Google Maps code. Despite my efforts and thorough research, I cannot seem to get it to work correctly. Below is the snippet of my code: <script type="text/javascript"> fu ...

Combine arrays using union or intersection to generate a new array

Seeking a solution in Angular 7 for a problem involving the creation of a function that operates on two arrays of objects. The goal is to generate a third array based on the first and second arrays. The structure of the third array closely resembles the f ...

When should separate controllers be created for a list of values in a Laravel REST API?

Imagine I have a straightforward API for user registration. This API collects basic information like Name, email, state, gender, and marital status for each user. I already have database tables pre-populated with ids for state, gender, and marital status o ...

How to choose an item from a dropdown list using its item number with jQuery

Is there a way to select an item in a dropdown menu by its item number using jQuery? <select id="selectbox" style="width: 220px;"> <option value="Option 1">Option 1</option> <option value="Option 2">Option 2</option> < ...

A complete div component with a minimum height that is sandwiched between a fixed size header and

I'm currently facing a challenge with setting up my webpage due to the height of my elements causing a bit of a frenzy. Specifically, I have a specific layout in mind: At the top, there should be a header that has a fixed height (let's say 150p ...

Tips for making an input form that triggers an alert popup

After creating an HTML form with text input, utilizing Javascript for validation as shown below: I am trying to trigger an alert box thanking the user for entering data when the submit button is clicked. I have faced challenges in implementing this witho ...

The dynamic data graph generated by HIGHCHARTS Areaspline is not as effective as expected

I need help creating a Dynamic Areaspline graph, but the result is coming out strangely. Does anyone have any ideas on how to fix this and get a smooth series? Here is an example of the issue: http://jsfiddle.net/mchc59nb/1/ chart: { ...

Executing operations on subdocuments in Mongoose without having to fetch the parent

Currently, when I need to delete a subdocument, I follow this process: Post.findById(post_id).exec(function(err, post) { post.comments.remove({'_id': comment_id}); post.save(function(err) { res.end("Success!"); }); }); This method doe ...

Issue with JQM popup functionality in Firefox browser

I am currently utilizing JQM 1.3.1 and have a webpage featuring various popups located at the bottom of the page: <div data-role="page" data-title="Strategic Plans"> <div data-role="content" id="capbPlans" data-bind="cafeLiveScroll: { callbac ...

Querying a map within a Mongo function results in an empty variable when accessed outside of the function, but

Currently, I am facing an issue while trying to create an array of objects using the map function in Mongo and node/express JS. Strangely, when I utilize console.log outside the map function, the array appears empty. However, within the map function, each ...

Issue with OBJLoader showing a blank screen

Does anyone have experience with rendering OBJ files using three.js? I'm having trouble getting them to display on the screen and would appreciate any help or advice. The strange thing is that a simple cube renders perfectly in my project. For those ...

What is the appropriate way to utilize `render_template` from Flask within Angular?

I'm facing an issue where I need to implement different routes for various Angular Material tabs. I have attempted to directly call Flask from the template, as demonstrated below, but unfortunately, I am unable to invoke render_template from Angular. ...

Updating with Ajax is functional for radios and checkboxes, however it does not work for buttons

When a button is clicked, I want the form to process and update accordingly. HTML: <input type="button" value="5.00" name="updatefield" id="updatefield" class="chooseit"> I have also tried: <button name="updatefield" id="updatefield" class="ch ...

"Bootstrap 4 does not allow labels to be displayed inline with the input field

I'm currently working with Bootstrap 4 and Vue.js to populate an input form, but I'm struggling to get the label to appear inline and before the input type file. <div v-for="problem in problems"> <div class="row"& ...

Is it possible that React.createElement does not accept objects as valid react children?

I am working on a simple text component: import * as React from 'react' interface IProps { level: 't1' | 't2' | 't3', size: 's' | 'm' | 'l' | 'xl' | 'xxl', sub ...

A Smarter Approach to Updating Text Dynamically with JavaScript and Vue

Currently, I am utilizing Vue to dynamically update text by using setInterval() in combination with a data property. The method I have in place is functional, but I am curious if there exists a more efficient way to optimize it. Is the current code as stre ...

Toggling javascript functionality based on media queries

On one of my slides, I want to display images that are specific to different devices like iPhone, iPad, and desktop. I am looking for a way to filter these images based on the device. Using display:none won't work as it's JavaScript, but here is ...

What is the method to display Post values to the user without necessitating a page reload?

Currently, I am attempting to utilize Ajax to send the user's inputted data from a JSP page. Through Ajax, I have successfully transmitted the variable "vars" to the Jsp page. The output upon registration includes: Registration is Successful. Welcome ...

What is the best way to create a button with a dynamic background animation in React?

Looking to design a button with an animated background, possibly in gif or video format. An example of what I have in mind is the DOWNLOAD button on this website's main page: Ideally, I am hoping for a solution using React. ...

Removing accordion borders in Bootstrap and AngularJS can be achieved by customizing

Can anyone help me figure out how to hide the border in each accordion group that contains a panel and inner elements? I've tried using classes like .accordion and .inner but nothing seems to work. Any advice on where to find the specific class or ID ...