2

I created an "autocomplete dropdown" that is appended to a container when the input event is triggered. The trigger works pretty well. But a noticed the CSS class IS NOT set to the container element. Chrome tool dev I cannot see the CSS class that is set by code. This is a VUE jS application and the function is called in the mount() hook. I'm facing two problems:

1 - Dropdown appears in front of the top NAV elements but behind the main content elements: I have tried to force the code with z-index on the browser dev tool but it completely ignores it.
enter image description here

2 - it seems like some classes .autocomplete-items are set by code.

enter image description here

My code:

HTML

    <div class="autocomplete">
        <input id="myInput" v-model="currentInput" @keypress.space="saveChip" @keydown.delete="backspaceDelete" @keypress.enter="clear"
        type="text" name="myCountry" placeholder="Country">
      </div>

JS

function autocomplete(inp, arr) { 
      console.log('autocomplete -START')
  /*the autocomplete function takes two arguments,
  the text field element and an array of possible autocompleted values:*/
  var currentFocus; 
      
    

  /*execute a function when someone writes in the text field:*/
  inp.addEventListener("input", function(e) { 

      console.log('inp.addEventListener("input", function(e) -START')

      var a, b, i, val = this.value;
      /*close any already open lists of autocompleted values*/
      closeAllLists();
      if (!val) { return false;}
      currentFocus = -1;
      /*create a DIV element that will contain the items (values):*/
      a = document.createElement("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items");
      /*append the DIV element as a child of the autocomplete container:*/
      this.parentNode.appendChild(a);
      /*for each item in the array...*/
      for (i = 0; i < arr.length; i++) {
      var pos = arr[i].toUpperCase().indexOf(val.toUpperCase());
        /*check if the item starts with the same letters as the text field value:*/
        if (pos > -1) {
          /*create a DIV element for each matching element:*/
          b = document.createElement("DIV");
          /*make the matching letters bold:*/
          b.innerHTML = arr[i].substr(0, pos);
          b.innerHTML += "<strong>" + arr[i].substr(pos, val.length) + "</strong>";
          b.innerHTML += arr[i].substr(pos + val.length);
          /*insert a input field that will hold the current array item's value:*/
          b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
          /*execute a function when someone clicks on the item value (DIV element):*/
          b.addEventListener("click", function(e) {
              /*insert the value for the autocomplete text field:*/
              inp.value = this.getElementsByTagName("input")[0].value;
              /*close the list of autocompleted values,
              (or any other open lists of autocompleted values:*/
              closeAllLists();
          });
          a.appendChild(b);
        }
      }
  });
  /*execute a function presses a key on the keyboard:*/
  inp.addEventListener("keydown", function(e) { 
      console.log(' inp.addEventListener("keydown"')

      var x = document.getElementById(this.id + "autocomplete-list");
      if (x) x = x.getElementsByTagName("div");
      if (e.keyCode == 40) {
        /*If the arrow DOWN key is pressed,
        increase the currentFocus variable:*/
        currentFocus++;
        /*and and make the current item more visible:*/
        addActive(x);
      } else if (e.keyCode == 38) { //up
        /*If the arrow UP key is pressed,
        decrease the currentFocus variable:*/
        currentFocus--;
        /*and and make the current item more visible:*/
        addActive(x);
      } else if (e.keyCode == 13) {
        /*If the ENTER key is pressed, prevent the form from being submitted,*/
        e.preventDefault();
        if (currentFocus > -1) {
          /*and simulate a click on the "active" item:*/
          if (x) x[currentFocus].click();
        }
      } 
      console.log(' inp.addEventListener("keydown" -END')

  });
  function addActive(x) {
      console.log('addActive -START')

    /*a function to classify an item as "active":*/
    if (!x) return false;
    /*start by removing the "active" class on all items:*/
    removeActive(x);
    if (currentFocus >= x.length) currentFocus = 0;
    if (currentFocus < 0) currentFocus = (x.length - 1);
    /*add class "autocomplete-active":*/
    x[currentFocus].classList.add("autocomplete-active"); 
     
      console.log('addActive -END')

  }
  function removeActive(x) { 
      console.log('removeActive -START')

    /*a function to remove the "active" class from all autocomplete items:*/
    for (var i = 0; i < x.length; i++) {
      x[i].classList.remove("autocomplete-active");
    } 
      console.log('removeActive -END')

  }
  function closeAllLists(elmnt) { 
      console.log('closeAllLists -START')

    /*close all autocomplete lists in the document,
    except the one passed as an argument:*/
    var x = document.getElementsByClassName("autocomplete-items");
    for (var i = 0; i < x.length; i++) {
      if (elmnt != x[i] && elmnt != inp) {
        x[i].parentNode.removeChild(x[i]);
      }
    } 
      console.log('closeAllLists -END')

  }
  /*execute a function when someone clicks in the document:*/
  document.addEventListener("click", function (e) {
      closeAllLists(e.target);
  });
},
}, 
   mounted()
  {
       this.autocomplete(document.getElementById("myInput"), this.countries); 
  },

CSS

.autocomplete
  { 
  position: relative;
  display: inline-block; 
  width: 60%; 
  margin: auto auto; 

.autocomplete-items {
  position: absolute;
  border: 1px solid #d4d4d4;
  border-bottom: none;
  border-top: none;
  z-index: 10000; 
  background-color: turquoise;
  /*position the autocomplete items to be the same width as the container:*/
  top: 100%;
  left: 0;
  right: 0;
}

.autocomplete-items div {
  padding: 10px;
  cursor: pointer;
  background-color: #fff; 
  border-bottom: 1px solid #d4d4d4; 
}

/*when hovering an item:*/
.autocomplete-items div:hover {
  background-color: #e9e9e9; 
}

/*when navigating through the items using the arrow keys:*/
.autocomplete-active {
  background-color: DodgerBlue !important; 
  color: #ffffff; 
} 
input {
  border: 1px solid transparent;
  background-color: #f1f1f1;
  padding: 10px;
  font-size: 16px; 
  width: 100%; 
  margin: auto;
  border-radius: 3px;
}
  }  
 

This is the SRC sample on codepen enter link description here

1
  • 1
    Please check your css. I am not sure you use css or scss. if you use css, you made an issue in the usage of curly bracket for .autocomplete class Commented Mar 29, 2021 at 10:02

2 Answers 2

1

This happens probably because you didn't close the curly bracket for .autocomplete

.autocomplete
  { 
  position: relative;
  display: inline-block; 
  width: 60%; 
  margin: auto auto; 
                          // here is the missing bracket
.autocomplete-items {
Sign up to request clarification or add additional context in comments.

Comments

1

If you use scss, your css code format is correct. if not, you need to check your css again. for this, please check the above answer of @biberman.

From your attached screenshot, I can recognize there is an index issue in your css.

Please add z-index as 1000 or bigger for .autocomplete

and please set background for .autocomplete

.autocomplete
  { 
   position: relative;
   display: inline-block; 
   width: 60%; 
   margin: auto auto; 
   background-color: #fff;
   z-index: 1000
 }

1 Comment

Also, You need to check the difference of z-index of the image card and autocomplete dropdown element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.