Replacing AJAX results when new items are clicked

Replacing AJAX results when new items are clicked

Hey everyone. 

Currently I have two functions that call endpoints in AJAX and they return json files with member information.  These are seperated into two columns (Groups and Members) I can populate everything, however, what I want to happen is when a new different group is clicked, the old results are replaced with the new one instead of being added to the list, which is what is happening now.


  1. //Get JSON from LDAP for groups
  2. $(function displayGroupItems() {
  3. $('.group-submit').click(function(event) {
  4. $.ajax({
  5. url:'ldap/groups',
  6. method: 'GET',
  7. dataType: 'json',
  8. success: function(data) {
  9. $.each(data.groups, function(i, dn) {
  10. output = '<li class="dn-wrapper">' +'<span class="dn-class">'+ dn.dn +'</span>' + '</li>';
  11. $(output).appendTo('#dropdown');

  12. });
  13. }
  14. })
  15. });
  16. });

  17. $(function displayMemberItems() {
  18.   $(document).on('click','.dn-class', function(event) {
  19.   var textclass = $(this).text().replace("/=/g",'%3D').replace("/,/g", '%2C');
  20.   $.ajax({
  21.   url:'ldap/members/'+textclass,
  22.   method:'GET',
  23.   dataType: 'json',
  24.   success: function(pull) {
  25.   $.each(pull.members, function(u, uid) {
  26.   content = '<span>'+uid+'</span>';
  27.   $(content).appendTo('.ldap-users');
  28.   });
  29.   }
  30.   })
  31.   });
  32. });
I'm just not sure where to start. I was looking at replaceWith, but that doesn't seem to apply to this situation.  I will paginate the results, just trying to get all the basic functions down first.

Thank you.