Home Ask Login Register

Developers Planet

Your answer is one click away!

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.


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 )


    $("#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>\
        data:'get_all_msg=true&user=' + id ,
            $("#jd-chat").find(".jd-user:first .jd-body").append("<span style='display:block' class='me'> " + data + "</span>");

    removeItem = $(this).parents(".jd-header").attr("id");

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

    var box=$(this).parents(".jd-user,.jd-online");

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


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


Leave an answer

Quote of the day: live life