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 5956907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:19:30+00:00 2026-05-22T18:19:30+00:00

What I am trying to do is keep appending divs to #ajax_tweets when my

  • 0

What I am trying to do is keep appending divs to #ajax_tweets when my ajax request returns a success. What currently happens is each time the ajax function is run on a timer and the ajax return a success the desired latest div is shown on screen correctly. However when it runs the next time the previous div is overwritten and not appended too. Either I am appending incorrectly, or I am not sure.. being driving me crazy for a while now.

Thank you in advance.

My code:

$(document).ready(function() {
    window.setInterval(ajax_test, 10000);
    counter = 0;

    function ajax_test() {
        $(".tweets").html('Retrieving...');
        $.ajax({
            type: "POST",
            url: "assets/ajax/get_latest_tweets.php",
            data: "tid=" + id,
            timeout: 20000,
            success: function(msg) {
                if (msg != "") {
                    add_to_tweet_feed( msg );
                    id++;
                    alert(id + msg);
                }     
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(".tweets").html('Timeout contacting server..');
            }
        });
    }

    function add_to_tweet_feed ( msg ) {
        $("#ajax_tweets").append(msg);  
    }
});

Code returned from server:

<div id="'.$tid.'" class="t_feed">             
                               <div class="calander right">
                                <div class="calander_month"><span>'.$row['pub_month'].'</span></div>
                                <div class="calander_day"><span>'.$row['pub_day'].'</span> </div> 
                               </div>
                                <span class="ajax_tweet bold">'.$row['source'].'</span>
                                <p>'.$tweet.'</p>
                              </div><div class="clear_both></div>

Front end code:

           <div id="ajax_tweets" class="tweets">

           </div>
  • 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-05-22T18:19:31+00:00Added an answer on May 22, 2026 at 6:19 pm

    This line in your code actually clears out your whole container, that’s why you only have the last one in there:

    $(".tweets").html('Retrieving...');
    

    One solution could be to make your markup something like this:

    <div id="ajax_tweets" class="tweets">
        <div id="ajax_message"></div>
    </div>
    

    …and change your code to update that element and insert new tweets in front of it:

    $(document).ready(function() {
        window.setInterval(ajax_test, 10000);
        counter = 0;
    
        var $message = $("#ajax_message");
    
        function ajax_test() {
            $message.html('Retrieving...');
            $.ajax({
                type: "POST",
                url: "assets/ajax/get_latest_tweets.php",
                data: "tid=" + id,
                timeout: 20000,
                success: function(msg) {
                    $message.html('');
                    if (msg != "") {
                        add_to_tweet_feed( msg );
                        id++;
                    }     
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $message.html('Timeout contacting server..');
                }
            });
        }
    
        function add_to_tweet_feed ( msg ) {
            $(msg).insertBefore($message);
        }
    });
    

    Edit: Fixed a bug in the above code (bad usage of insertBefore()). Also, in case you’re worried about the order in which your requests come back, you can add placeholders and replace them on success or remove them on failure:

    $(document).ready(function() {
        window.setInterval(ajax_test, 10000);
        counter = 0;
    
        var $message = $("#ajax_message");
    
        function ajax_test() {
            $message.html('Retrieving...');
    
            var $placeholder = $('<div></div>').insertBefore($message).hide();
    
            $.ajax({
                type: "POST",
                url: "assets/ajax/get_latest_tweets.php",
                data: "tid=" + id,
                timeout: 20000,
                success: function(msg) {
                    $message.html('');
                    if (msg != "") {
                        add_to_tweet_feed(msg, $placeholder);
                        id++;
                    }     
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $placeholder.remove();
                    $message.html('Timeout contacting server..');
                }
            });
        }
    
        function add_to_tweet_feed (msg, $placeholder) {
            $placeholder.show().replaceWith(msg);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently struggling a bit trying to keep my controller specs DRY and succinct
i am trying to play a stream in real time ( I keep appedning
I'm trying to keep a file updated real time with the server. Its more
I am trying to create a file and keep on appending into that file.
In trying to keep with unobtrusive JavaScript guidelines I moved all the JavaScript out
I am trying to keep elements in header in one row. My current styles
I'm trying to keep my code clean and keep the number of files down.
I'm trying to keep my xsl DRY and as a result I wanted to
I'm trying to keep Visual Studio from attempting to serialize the datasource of drop
I'm trying to keep the list of values from drop-down selections when the browser

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.