title: load data in custom dropdown using JavaScript, Ajax
body: <p>I am working on web application built on Spring Boot and using JavaScript as a frontend technology. So here I am facing a problem on my index page. There are dropdowns on which after clicking there are some options coming from API. The problem is, when my index page loads then if user clicks on dropdown to choose some category then data does not loaded. It is a custom dropdown.</p>
  <p>This is an HTML of dropdown:</p>
  <pre><code>&lt;div class=&quot;col-6&quot;&gt;
      &lt;label class=&quot;sr-only&quot; for=&quot;inlineFormInputGroup&quot; style=&quot;color: red;&quot;&gt; &lt;/label&gt;
        &lt;div class=&quot;custom-select-div&quot;&gt;
          &lt;select id=&quot;category&quot; name=&quot;category&quot;&gt;
              &lt;option value='0'&gt;Choose your category&lt;/option&gt;
          &lt;/select&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Below is my JavaScript code for custom dropdown:</p>
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          setTimeout(function(){
              getcategorylist();
          });
              
          setTimeout(function(){
              var x, i, j, l, ll, selElmnt, a, b, c;
              x = document.getElementsByClassName(&quot;custom-select-div&quot;);
              l = x.length;
              for (i = 0; i &lt; l; i++) {
                 selElmnt = x[i].getElementsByTagName(&quot;select&quot;)[0];
                 ll = selElmnt.length;
                  a = document.createElement(&quot;DIV&quot;);
                  a.setAttribute(&quot;class&quot;, &quot;select-selected&quot;);
                  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
                  x[i].appendChild(a);
                  b = document.createElement(&quot;DIV&quot;);
                  b.setAttribute(&quot;class&quot;, &quot;select-items select-hide&quot;);
                  for (j = 1; j &lt; ll; j++) {
                     c = document.createElement(&quot;DIV&quot;);
                     c.innerHTML = selElmnt.options[j].innerHTML;
                     c.addEventListener(&quot;click&quot;, function(e) {
                      var y, i, k, s, h, sl, yl;
                      s = this.parentNode.parentNode.getElementsByTagName(&quot;select&quot;)[0];
                      sl = s.length;
                      h = this.parentNode.previousSibling;
                      for (i = 0; i &lt; sl; i++) {
                         if (s.options[i].innerHTML == this.innerHTML) {
                            s.selectedIndex = i;
                            h.innerHTML = this.innerHTML;
                            y = this.parentNode.getElementsByClassName(&quot;same-as-selected&quot;);
                            yl = y.length;
                            for (k = 0; k &lt; yl; k++) {
                               y[k].removeAttribute(&quot;class&quot;);
                            }
                           this.setAttribute(&quot;class&quot;, &quot;same-as-selected&quot;);
                          break;
                           }
                       }
                      h.click();
                    });
                    b.appendChild(c);
                  }
                    x[i].appendChild(b);
                    a.addEventListener(&quot;click&quot;, function(e) {
                      e.stopPropagation();
                      closeAllSelect(this);
                      this.nextSibling.classList.toggle(&quot;select-hide&quot;);
                      this.classList.toggle(&quot;select-arrow-active&quot;);
                    });
                  }
  
                  function closeAllSelect(elmnt) {
                    var x, y, i, xl, yl, arrNo = [];
                    x = document.getElementsByClassName(&quot;select-items&quot;);
                    y = document.getElementsByClassName(&quot;select-selected&quot;);
                    xl = x.length;
                    yl = y.length;
                    for (i = 0; i &lt; yl; i++) {
                      if (elmnt == y[i]) {
                        arrNo.push(i)
                      } else {
                        y[i].classList.remove(&quot;select-arrow-active&quot;);
                      }
                    }
                    for (i = 0; i &lt; xl; i++) {
                      if (arrNo.indexOf(i)) {
                        x[i].classList.add(&quot;select-hide&quot;);
                      }
                    }
                  }
                  document.addEventListener(&quot;click&quot;, closeAllSelect);
              },500);
          });
          
          function getcategorylist(){ 
              var maincategory = document.getElementById('maincategory').value;
              $.post(&quot;&lt;%=AkApiUrl.getcategorybymaincategory%&gt;&quot;,{
                  maincategory : maincategory,
              },
              function(data, status) {
                  if (data.status == &quot;OK&quot;) {
                      if (data.statusCode == 1) { 
                      debugger
                          var list = data.response;
                          var categorys='';
                          if(list.length &gt; 0){
                              for (var i = 0; i &lt; list.length; i++) {                             
                                  var content = &quot;&lt;option value='&quot;+list[i].categoryid+&quot;'&gt;&quot;+list[i].name+&quot;&lt;/option&gt;&quot;;
                                  categorys = categorys + content;
                              }
                          }
                          categorys = &quot;&lt;option value='0' style='display: none;'&gt;Choose your category&lt;/option&gt;&quot;+categorys;
                          document.getElementById('category').innerHTML = categorys;
                      }else {
                          var error = data.responseMessage;
                          swal(error, &quot;&quot;, &quot;error&quot;);
                      }
                  } else {
                      var error = data.responseMessage;
                      swal(error, &quot;&quot;, &quot;error&quot;);
                  }
              });
          }
          
          $(&quot;#searchBtn&quot;).click(function() {
              var maincategoryid = document.getElementById('maincategory').value;
              var latitude = document.getElementById(&quot;latitude&quot;).value;
              var longitude = document.getElementById(&quot;longitude&quot;).value;
              var location = document.getElementById(&quot;address&quot;).value;
              var categoryid = document.getElementById(&quot;category&quot;).value;
              var category = $(&quot;#category option:selected&quot;).text();
              
              var maincategory = &quot;Truck-Trailer&quot;;             
              if(maincategoryid == '1'){
                  maincategory = &quot;Truck-Trailer&quot;;
              }else if(maincategoryid == '2'){
                  maincategory = &quot;Cars&quot;;
              }else if(maincategoryid == '3'){
                  maincategory = &quot;RV-Bus&quot;;
              }else {
                  maincategory = &quot;Truck-Trailer&quot;;
              }
              
              if (latitude == &quot;&quot; || latitude == null || longitude == &quot;&quot; || longitude == null || categoryid == 0) {
                  if (latitude == &quot;&quot; || latitude == null || longitude == &quot;&quot; || longitude == null) {
                      $(&quot;#location&quot;).addClass(&quot;errorInput&quot;);
                      return false;
                  } else {
                      $(&quot;#location&quot;).removeClass(&quot;errorInput&quot;);
                  }
                  
                  if (categoryid == 0) {
                      $(&quot;#category&quot;).addClass(&quot;errorInput&quot;);
                      return false;
                  } else {
                      $(&quot;#category&quot;).removeClass(&quot;errorInput&quot;);
                  }
              }else{
                  category = category.replaceAll(&quot; &amp; &quot;, &quot;-&quot;).replaceAll(&quot; &quot;, &quot;-&quot;);
                  location = location.replaceAll(&quot;, &quot;, &quot;-&quot;).replaceAll(&quot;&amp;&quot;, &quot;-&quot;).replaceAll(&quot; &quot;, &quot;_&quot;);
                  document.getElementById(&quot;listingForm&quot;).action = &quot;&lt;%=WebUrl.listing%&gt;/&quot;+maincategory+&quot;/&quot;+category+&quot;/&quot;+location;
                  document.getElementById(&quot;listingForm&quot;).submit();
              }           
           });
      &lt;/script&gt;
  </code></pre>
  <p><strong>Data did not get loaded on first time when DOM loaded after refreshing page from 1-2 times then sometimes data get visible inside the dropdown. So what should I do?</strong></p>