Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
¿Cómo revertir los cambios de DOM haciendo clic fuera de la entrada de búsqueda?

Cuando hago clic en la entrada de búsqueda, funciona como se desea.

Pero cuando vuelvo a hacer clic en la entrada de búsqueda, se invierte el efecto del primer clic.

Debería revertir el efecto del primer clic cuando hago clic fuera de la entrada de búsqueda.

Aquí está mi código:

 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 answers
Answer question

0

En lugar de usar toggle() , use add() y remove() para que cuando haga clic dos veces en el elemento de input , el foco no se apague

Agregue esto a su código js . Si hizo clic en cualquier elemento que no es el elemento de input , las clases se eliminan y vuelve a la normalidad

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

Código completo:

 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 Report

0

Lo que puedes hacer es:

 <input type="search" name="" id="inpt" onfocus="active()" onblur="deactivate()">
about 4 years ago · Santiago Trujillo Report

0

Puedes usar onfocus y desenfoque

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!