Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
How to reverse DOM changes by clicking outside of search input?

When I click into the search input, it works as desired.

But when I click into the search input again, it reverses the effect of the first click.

It should reverse the effect of the first click when I click outside the search input.

Here is my code:

function active() {
  let body = document.body;
  let inp = document.getElementById("inpt")
  body.classList.toggle("bg")
  inp.classList.toggle("iwidth")
}
input {
  outline: none;
  width: 150px;
  padding: 2px;
}

.iwidth {
  width: 300px;
}

.bg {
  background: rgba(0, 0, 0, 0.8);
  transition: 300ms;
}
<input type="search" name="" id="inpt" onclick="active()">

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Instead of using toggle(), use add() and remove() so that when you click on the input element twice, the focus doesn't go off

Add this to your js code. If you clicked on any element which is not the input element, the classes are removed and it goes back to normal

document.addEventListener("click", function (e) {
    if (e.target.id != "inpt") {
        inactive();
    }
})

Whole code:

function active() {
  let body = document.body;
  let inp = document.getElementById("inpt")
  body.classList.add("bg")
  inp.classList.add("iwidth")
}

function inactive() {
  let body = document.body;
  let inp = document.getElementById("inpt")
  body.classList.remove("bg")
  inp.classList.remove("iwidth")
}

document.addEventListener("click", function(e) {
  if (e.target.id != "inpt") {
    inactive();
  }
})
input {
  outline: none;
  width: 150px;
  padding: 2px;
}

.iwidth {
  width: 300px;
}

.bg {
  background: rgba(0, 0, 0, 0.8);
  transition: 300ms;
}
<input type="search" name="" id="inpt" onclick="active()">

about 4 years ago · Santiago Trujillo Denunciar

0

What you can do is:

<input type="search" name="" id="inpt" onfocus="active()" onblur="deactivate()">

about 4 years ago · Santiago Trujillo Denunciar

0

You can use onfocus and on blur

function active() {
            let body = document.body;
            let inp = document.getElementById("inpt")
            body.classList.toggle("bg")
            inp.classList.toggle("iwidth")
        }
input {
    outline: none;
    width: 150px;
    padding: 2px;
}
.iwidth {
    width: 300px;
}
.bg {
    background: rgba(0, 0, 0,0.8);
    transition: 300ms;
}
<input type="search" name="" id="inpt" onfocus="active()" onblur="alert('deactivate')">

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda