Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that youdelete everything from usernames and after that you write all contacts. You would better remove the offline contacts from the $('#usernames') and after that to add to that list the online contacts. I writed some functions to show you the functionality. I created html list of online contacts and I also created an array of new online contacts. Here is the code: </p> <pre><code>&lt;div id="u"&gt; &lt;div class="d" onclick="chat('asd1')"&gt;asd1&lt;/div&gt; &lt;div class="d" onclick="chat('asd12')"&gt;asd12&lt;/div&gt; &lt;div class="d" onclick="chat('asd13')"&gt;asd13&lt;/div&gt; &lt;div class="d" onclick="chat('asd142')"&gt;asd14&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Here you have the javascript that you need to run after the DOM is ready:</p> <pre><code>var onlineUsernames = ["asd211","asd12","asd13","asd14"]; var usernamesContainerID = 'u'; var $usernamesContainer = $('#'+usernamesContainerID); function extractUsernameFromAttr(onclickValue) { return onclickValue.split("'")[1]; } function buildExistingUsernames($userDivs) { var existingUsernames = []; $userDivs.each(function(index,value){ var username = extractUsernameFromAttr($userDivs[index].getAttribute('onclick')); existingUsernames.push(username); }) return existingUsernames; } function removeUserFromList($user) { document.getElementById(usernamesContainerID).removeChild($user); } function addUserToList(value) { $('&lt;div/&gt;',{ onclick:"chat('"+value+"')", class :'d', text:value }).appendTo($usernamesContainer); } function deleteOfflineContacts(existingUsernames,usernames,$userDivs) { $.each(existingUsernames,function(index,value) { if($.inArray(value,usernames)==-1) { removeUserFromList($userDivs[index]); } }) } function addOnlineContacts(existingUsernames,usernames) { $.each(usernames,function(index,value) { if($.inArray(value,existingUsernames)==-1) { addUserToList(value); } }) } function update($userDivs) { var existingUsernames = buildExistingUsernames($userDivs); deleteOfflineContacts(existingUsernames,onlineUsernames,$userDivs); addOnlineContacts(existingUsernames,onlineUsernames); } var $userDivs = $usernamesContainer.children("div"); setTimeout(function() { update($userDivs); },3000); </code></pre> <p>If you need it here is a working example: <a href="http://jsfiddle.net/9gRyQ/2/" rel="nofollow">http://jsfiddle.net/9gRyQ/2/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload