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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:17:03+00:00 2026-05-26T11:17:03+00:00

Question please: var ids = [‘user_1’, ‘user_2’, ‘user_3’, ‘user_666’]; My HTML <div id=chatUsers> <div

  • 0

Question please:

var ids = ['user_1', 'user_2', 'user_3', 'user_666'];   

My HTML

<div id="chatUsers">
   <div id="user_1">username-1</div>
   <div id="user_2">username-2</div>
   <div id="user_5">username-5</div>
</div>

How to using jQuery make my container looks like this:

<div id="chatUsers">
   <div id="user_1">username-1</div>
   <div id="user_2">username-2</div>
   <div id="user_3">username-3</div>
   <div id="user_666">username-666</div>
</div>

Remove div with id="user_5" (we have no this in our ids list) and add div with id=”user_666″ (in our ids list).

Thank you very much!

I’m tried something like that, but this not work:

jQuery('#chatUsers').filter(function() {
   var a = jQuery(this).find('div');
   alert ("Filtering: divs count: " + a.length);
   //if (!a.length)
   //return true;
   var id =  a.attr('id');  
   if (jQuery.inArray(id,  ids) >= 0)
      return false;
   return true;
}).remove();
  • 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-05-26T11:17:04+00:00Added an answer on May 26, 2026 at 11:17 am

    Something like this would work, and avoid disrupting existing items:

    var ids = ['user_1', 'user_2', 'user_3', 'user_666'];   
    
    $('#chatUsers').children().filter(function() {
         return ids.indexOf(this.id) === -1;
    }).remove();
    
    var length = ids.length;
    
    for (var i = 0; i < length; i++) {
        if (!document.getElementById(ids[i])) {
            $('<div>', { id: ids[i], text: ids[i] }).appendTo('#chatUsers');    
        }
    }
    

    It doesn’t set the text exactly right when appending new ones. I’m assuming in your real code, you’ve got an object property in the array that you can use for that.

    It also doesn’t order them when appending, but that would be possible with insertAfter().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A basic question please, I am trying this code: var number_questions = postsJSON1[i]['question'].length; for
Quick rookie question please. Suppose I have a javsacript object like so: var meh=[[cars,27],
PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE. I have
Related to my other question , please help me debug An unhandled exception of
Can anyone help me with a Javascript question, please? Why does the following code
(please read the update section below, I leave the original question too for clarity)
This is my first post, so please forgive me if this question has been
First of all, i'm a Graphic designer so please ignore if this programming question
I am not sure if I have asked the question correctly. Please correct me
yes, this is a homework-type question but could you please help me out? In

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.