Choosing divs obtained through AJAX in jquery

I am creating a basic forum where each post contains the name of the author, the text and the date it was created. I would like the forum to be constantly updated via AJAX and show new posts that were created on the fly. Currently, I have a PHP file getlatest.php?lastid=...that retrieves all messages from the identifier specified for the most recent one. It returns data in HTML, for example (I changed it so that you can see divs, stackoverflow throws it away):


foreach ($print as $value)
{
    $readyText .= div id = $value->post_id;
    $readyText .= $value->first_name.' '.$value->last_name.' posted the following:'.
    $value->post_text.' The post was made about '.$time.' ago. 
    $readyText .= '/div>'; 
}

I have some AJAX code in jquery that retrieves every few moments


setInterval("update()", 3000);
            function update()
            {
                $.get("getlatest.php", 
                {
                    id: latestmessage
                }, 
                function(response){
                    $("#forum_entries").prepend(response);
                    latestmessage = $.cookie('last_post_id'); //This is
                                      //how I know what the latest post id is
                }, "html");

I wanted to highlight all the new messages that were sent using the (now very popular) yellow wilting technology, for example

$ ("# somediv"). effect ("highlight", {}, 1500);

- div ? PHP, div, PK .

+3
3

, prepend prependTo. PrependTo , , ( jQuery 1.3.2).

  $.get('getlatest.php',
        { id: latestmessage }, 
        function(response) {
            $(response).prependTo('#forum_entries').effect('highlight',{},1500);
            latestmessage = $.cookie('last_post_id');
        }, 'html' );
+5

Div :

<?php

foreach ($print as $value)
{
    $readyText .= '<div id = "' . $value->post_id . '" class="active"';
    $readyText .= $value->first_name.' '.$value->last_name.' posted the following:'.
    $value->post_text.' The post was made about '.$time.' ago. 
    $readyText .= '</div>'; 
}

?>



setInterval("update()", 3000);
            function update()
                {
                $.get("getlatest.php", 
                        {
                    id: latestmessage
                }, 
                        function(response){
                    $("#forum_entries").prepend(response);
                    latestmessage = $.cookie('last_post_id');
            $("div.active").effect("highlight", {}, 1500);
            $("div.active").toggleClass("active");

                }, "html");

, JavaScript / ( jQuery, ).

+1

In a loop, you can add a fake class attribute for newer div messages ... then in your ajax call after adding to #forum_entries you can apply the selection, remove the removeAttr class attribute (class name) in JQuery. so the next time you run, you won’t have a problem.

0
source

Source: https://habr.com/ru/post/1711660/


All Articles