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

318
Views
Cómo evitar DOMException no detectada al verificar el selector en document.querySelector

en JavaScript tengo un código como:

 var text = '[0-9]'; if (document.querySelector(text) !== null) { alert('fail'); }

Cuando lo ejecuto, hay un error:

DOMException no detectada: no se pudo ejecutar 'querySelector' en 'Documento': '[0-9]' no es un selector válido.

Por favor, ¿cómo verificar si querySelector es válido o no válido sin este error?

Quiero tener la capacidad de escribir cualquier cosa en la variable "texto" y quiero que el código avise "fallido" cuando no es un selector válido. Sin ese error, no importa qué variable contenga "texto".

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar try... catch para evitar el error

 let text = '[0-9]'; if (getQuerySelector(text) !== null) { alert('fail'); } function getQuerySelector(txt){ try { return document.querySelector(txt); } catch(e) { return null; } }
about 4 years ago · Juan Pablo Isaza 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!