Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:25:35+00:00 2026-06-04T03:25:35+00:00

So I am trying to loop through some elements and change some text based

  • 0

So I am trying to loop through some elements and change some text based on the result of an ajax call. the problem is I cannot get the data out of the ajax callback and I am not sure how exactly to chain the events to do so. I am getting a stock quote value it would be nice if I could just return that object up into the previous scope, the loop of matches, and then do all the manipulation there.

$(function(){
          var tweets = $('.tweet');
          var symbol_pat = /(^|\s\$)([a-z]+\b)/gi;
          $.each(tweets, function(){
            var tweet_html = $(this).html();
            tweet_html = tweet_html.replace(symbol_pat,function(){
               var symbol = arguments[2];
               var YAHOO_API_URL = 'http://query.yahooapis.com/v1/public/yql'
               var format = 'json'
               var query = 'select * from yahoo.finance.quotes where symbol in ("'+symbol+'")';
               var env = "store://datatables.org/alltableswithkeys";

               $.ajax({
                    'url':YAHOO_API_URL,
                    'async':false,
                    'method':'GET',
                    'data': {
                        'format':format,
                        'q':query,
                        'env':env
                    },
                    success: function(data){
                        var quote = data.query.results.quote;
                        var change = quote.Change;
                        var change_pct = quote.ChangeinPercent;
                        var quote_price = quote.LastTradePriceOnly;
                        var html_str = "";

                        if( change.indexOf("+") != -1 ){
                            html_str = '<span class="symWrap up">'+arguments[0]+'</span>';
                        }else{
                            html_str = '<span class="symWrap down">'+arguments[0]+'</span>';
                        }

                        tweet_html = arguments[0].replace(html_str);
                        $(this).html(tweet_html);
                    }
               });

            });
          });

        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:25:37+00:00Added an answer on June 4, 2026 at 3:25 am

    $.ajax() runs asynchronously, so you can’t really “wait” for the success in the previous scope. You can use jQuery promise and Deferred to do this though. Check out http://www.erichynds.com/jquery/using-deferreds-in-jquery/ and http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/

    EDIT:
    showing an alternate solution that doesn’t require promise or deferred:

    $(function(){
      var tweets = $('.tweet');
      var symbol_pat = /(^|\s\$)([a-z]+\b)/gi;
      $.each(tweets, function(){
           var that = this;
    
           var symbol = arguments[2];
           var YAHOO_API_URL = 'http://query.yahooapis.com/v1/public/yql'
           var format = 'json'
           var query = 'select * from yahoo.finance.quotes where symbol in ("'+symbol+'")';
           var env = "store://datatables.org/alltableswithkeys";
    
           $.ajax({
            'url':YAHOO_API_URL,
            'async':false,
            'method':'GET',
            'data': {
            'format':format,
            'q':query,
            'env':env
            },
            success: function(data){
            var quote = data.query.results.quote;
            var change = quote.Change;
            var change_pct = quote.ChangeinPercent;
            var quote_price = quote.LastTradePriceOnly;
            var html_str = "";
    
            if( change.indexOf("+") != -1 ){
                html_str = '<span class="symWrap up">'+arguments[0]+'</span>';
            }else{
                html_str = '<span class="symWrap down">'+arguments[0]+'</span>';
            }
    
                var tweet_html = $(that).html();
                    var tweet_html = arguments[0].replace(html_str);
                tweet_html = tweet_html.replace(symbol_pat,html_str);
            $(that).html(tweet_html);
            }
           });
    
        });
      });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through some elements in a form (radios, text boxes,
Just trying to get some rows out of a database and loop through but
I'm trying to set properties of some elements through a for loop, but am
I'm trying to create a script that will loop through some data frames and
I'm currently trying to write some Javascript to loop through the elements in a
I am trying to loop through a directory of text files and combine them
I am trying to loop through a vectors elements and trying to find out
I am trying to loop through this external JSON file(locally stored), but I cannot
I'm trying to loop through a text file with a list of urls and
I am trying to loop through some controls in a Powershell / WPF application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.