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

  • Home
  • SEARCH
  • 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 8984879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:12:45+00:00 2026-06-15T21:12:45+00:00

I am currently coding an instant chatbox using jquery which will show the latest

  • 0

I am currently coding an instant chatbox using jquery which will show the latest chat on top (refreshes when user send data via post request)
and push the oldest chat downward and remove it.

The problem is that if more than one latest chat is retrieved(for example, 2), two new div will be prepended but only one oldest div is removed instead of two…I tried timeout but it didnt work either..

Below are the code snippets I believe which got problem in it.

function showData(currentchatstyle, data, final){
   var newchatstyle;
    if (currentchatstyle == "chatone") {
        newchatstyle = "chattwo";
    }
    else {
        newchatstyle = "chatone";
    }

    $('div[class^="chat"]:first').before('<div class="' + newchatstyle + '" style="display:none;">' + data + ' </div>');
    $('div[class^="chat"]:first').slideDown(500,"swing", function(){
        $('div[class^="chat"]').last().fadeOut(500, function() {
            $(this).remove(); 
        });
    });

    return newchatstyle;
}

$('input[name="content"]').keyup(function(key) {
    if (key.which==13) {

        var author = $('input[name="author"]').val();
        var content = $('input[name="content"]').val();
        var lastnum = $('postn:first').text();
        var chatstyle = $('div[class^="chat"]:first').attr("class");

        $.post(
            "chatajax.php",
            { "author": author, "content": content, "lastnum": lastnum }, 
            function(data) {
                var msg = data.split("|~|");
                for (var i = 0; i < msg.length; i++) {
                    chatstyle = showData(chatstyle, msg[i], true);
                }
            }
        ); 
    }
});

Help will be very much appreciated.

  • 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-15T21:12:47+00:00Added an answer on June 15, 2026 at 9:12 pm

    The problem is that you do select also currently-fading-out divs with $('div[class^="chat"]').last(), as you don’t remove them immediately but in the animation callback. You for example might immediately remove the chat class so it won’t be selected in the next call to showData.

    Also, you should only use one class “chat” for a similar divs and for a zebra-style give them independent classes.

    var chatstyle = "one";
    function showData(data, final){
        chatstyle = chatstyle=="one" ? "two" : "one";
        var newDiv = $('<div class="chat '+chatstyle+'" style="display:none;">'+data+'</div>');
        $('div.chat:first').before(newDiv);
        newDiv.slideDown(500, "swing", function(){
            $('div.chat:last').removeClass('chat').fadeOut(500, function() {
    //                        ^^^^^^^^^^^^^^^^^^^^
                $(this).remove(); 
            });
        });
    }
    function post(data) {
        return $.post(
            "chatajax.php",
            data,
            function(data) {
                var msg = data.split("|~|");
                for (var i = 0; i < msg.length; i++)
                    showData(msg[i], true); // what's "final"?
            }
        );
    }
    
    $('input[name="content"]').keyup(function(key) {
        if (key.which==13)
           post({
               "author": $('input[name="author"]').val(),
               "content": $('input[name="content"]').val(),
               "lastnum": $('postn:first').text() // I'm sure this should not be extracted from the DOM
           });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently coding a product which will have only one database but will host
I'm currently coding a button which will delete a record from a database if
I am currently coding an Android app, which will be used to count Traffic
I am currently coding setup.py using setuptools. And I want to copy the static
I am currently coding some chat program and now want to play music via
I'm currently using a JavaScript coding style based on Douglas Crockford's Code Conventions for
I'm currently working on some coding for a hotel booking widget and am using
I am currently coding a Java WebService using axis2. However, one particular request needs
I am currently coding test with dbunit(using hsqldb). However, I have a huge Problem
I am currently coding a gui and want to verify that the user fills

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.