David Concha February 2016

get message without re open div

Im working in a chat system very simple. But i have a problem. I can only get the other user messages if i close and re open the chat conversation div, then the message appears.

How can I solve this? The problem should be in this piece of code. As I am not very comfortable with ajax I ask for your help.

JS:

 $(document).ready(function(){
var snd = new Audio("images/new_msg.wav"); 
var open=Array();




$("#jd-chat .jd-online_user").click(function(){
    var user_name = $.trim($(this).text());
    var id = $.trim($(this).attr("id"));

    if($.inArray(id,open) !== -1 )
        return

    open.push(id);

    $("#jd-chat").prepend('<div class="jd-user">\
        <div class="jd-header" id="' + id + '">' + user_name + '<span class="close-this"> X </span></div>\
        <div class="jd-body"></div>\
        <div class="jd-footer"><input id="textareabox" placeholder="escrever..."></div>\
    </div>');
    $.ajax({
        url:'chat.class.php',
        type:'POST',
        data:'get_all_msg=true&user=' + id ,
        success:function(data){
            $("#jd-chat").find(".jd-user:first .jd-body").append("<span style='display:block' class='me'> " + data + "</span>");

        }
    });
});
$("#jd-chat").delegate(".close-this","click",function(){
    removeItem = $(this).parents(".jd-header").attr("id");
    $(this).parents(".jd-user").remove();

    open = $.grep(open, function(value) {
      return value != removeItem;
    }); 
});

$("#jd-chat").delegate(".jd-header","click",function(){
    var box=$(this).parents(".jd-user,.jd-online");
    $(box).find(".jd-body,.jd-footer").slideToggle();
});

$("#search_chat").keyup(function(){
    var val =  $.trim($(this).val());
    $(".jd-online .jd-body").find("span").each(function(){
        if ($(this).text().search(new RegExp(val, "i")) < 0 ) 
        {
            $(this).fadeOut(); 
        } 
        else 
        

Answers


Pierre Granger February 2016

How messages are displayed when you first open the chat conv div ?

I'm assuming message_cycle() is supposed to display new messages in your div every second, i suppose your problem is here...

I'm not really confortable with this :

var box = $("#jd-chat").find("div#2").parents(".jd-user");

A div can't have an id starting with a number, you need <div id="chat2"> instead of <div id="2">.

After your line add a console.log('Box found : '+box.length) ; just to make sure your box is correctly found in message_cycle.

Post Status

Asked in February 2016
Viewed 2,098 times
Voted 6
Answered 1 times

Search




Leave an answer