Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
onfocusout event removes my button which cannot execute its code, because the focus removed when I click it

I am trying to implement a text field with dropdown search. The drop down items are buttons inside a div which has display: none CSS property. When I click it, The text field should change its content to the content of the button.

The problem is that the dropdown should disappear when the focus is removed from text field. When I click an item of dropdown, the focus is removed, the button disappears and its onclick doesn't get executed. Is it possible to execute button code which selects name before executing the code which removes this button?

function showNames() {
  document.getElementById("dropdown_names").style.display = 'block'
}

function hideNames() {
  document.getElementById("dropdown_names").style.display = 'none'
}

function selectName(name) {
  document.getElementById("name_inp").value = name
}
#resident_name_inp:focus {
  outline: 3px solid #ddd;
}

.dd {
  position: relative;
  display: inline-block;
}

.dd-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  overflow: auto;
  width: 100%;
  border: 1px solid #ddd;
  z-index: 1;
}

.dd-content button {
  background-color: #ffffff;
  width: 100%;
  padding-left: 4px;
  border: none;
  display: block;
}

.dd-content button:hover {
  background-color: #ddd;
}
<div class="dd">
  <input
  id="name_inp"
  type="text"
  placeholder="Search Names..."
  onfocus="showNames()"
  onfocusout="hideNames()">
  <div id="dropdown_names" class="dd-content">
    <button type="button" onclick="selectName('alex')">alex</button>
    <button type="button" onclick="selectName('jake')">jake</button>
    <button type="button" onclick="selecttName('evgen')">evgen</button>
  </div>
</div>

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

You can use relatedTarget to check if you clicked on a button element related to the input. Heres what to edit:

function hideNames() {
  if(event.relatedTarget != null){
    if(event.relatedTarget.tagName == 'BUTTON'){
      return
    }
  }
    document.getElementById("dropdown_names").style.display = 'none'
}

function selectName(name) {
  document.getElementById("name_inp").value = name
  document.getElementById("dropdown_names").style.display = 'none'
}
about 4 years ago · Santiago Gelvez Relatório

0

ES6 solution using arrow functions, works for multiple dropdowns, and lots of comments in the JS to explain what's happening.

Also redid the styling to use CSS grid.

/* Get array of all .dd elements */
const dropdowns = Array.from(document.getElementsByClassName('dd'));

/*For each .dd element */
dropdowns.forEach(dropdown => {
  const input = dropdown.firstElementChild;
  const names = dropdown.lastElementChild;

  /* Add click event listener to <input> of .dd */
  input.addEventListener('click', () => dropdown.classList. toggle('show') );

  /* For each button in .dd-content */
  Array.from(names.children).forEach(option => {
    /* Add click event listener button */
    option.addEventListener('click', () => {
      /* Set input value to button value */
      input.value = option.innerHTML;

      /* Remove .show class from dropdown */
      dropdown.classList.remove('show');
    })
  });
});
.dd { display: grid }

.dd input { text-align: center }
.dd input { cursor: s-resize } .dd.show input { cursor: n-resize }

.dd .dd-content {
  grid-row-start: 2;
  border: 1px solid #ddd;
  display: none;
}
.dd.show .dd-content { display: block }

.dd .dd-content button {
  background-color: #ffffff;
  width: 100%;
  padding-left: 4px;
  border: none;
  display: block;
}

.dd .dd-content button:hover { background-color: #ddd }
<div class="dd">
  <input type="text" placeholder="Search Names...">
  <div class="dd-content">
    <button type="button">alex</button>
    <button type="button">jake</button>
    <button type="button">evgen</button>
  </div>
</div>

<hr>

<div class="dd">
  <input type="text" placeholder="Some Thing Else">
  <div class="dd-content">
    <button type="button">Some</button>
    <button type="button">Thing</button>
    <button type="button">Else</button>
  </div>
</div>

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda