  
      function dismissRelatedLookupPopup(win, chosenId) {
          var name = windowname_to_id(win.name);
          var elem = document.getElementById(name);
          if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) {
              elem.value += ',' + chosenId;
          } else {
              document.getElementById(name).value = chosenId;
          }
          win.close();
 
</s> remove                 if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) {
 </s> add                 if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) { </s> remove                 if (inp.type === 'text' && inp.className.match(/vTimeField/)) {
 </s> add                 if (inp.type === 'text' && inp.classList.contains('vTimeField')) { </s> remove         element.className += " type-active";
 </s> add         element.classList.add("type-active"); </s> remove                 if (elem.className.indexOf('active') !== -1) {
 </s> add                 if (elem.classList.contains('active')) { </s> remove                 if (ps[i].className.indexOf("info") !== -1) {
 </s> add                 if (ps[i].classList.contains("info")) {
        if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) {
  
      function dismissRelatedLookupPopup(win, chosenId) {
          var name = windowname_to_id(win.name);
          var elem = document.getElementById(name);
        if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) {
              elem.value += ',' + chosenId;
          } else {
              document.getElementById(name).value = chosenId;
          }
          win.close();
keep keep keep keep replace keep keep keep keep keep
 Fixed #31028 -- Used classList API to check, add and remove DOM classes.

Thanks to Claude Paroz for review. 
django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js