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

550
Vistas
How to access dom element that is not inside JSX in React?

We are using ReactJS framework, so we don't find element written in the entire code. The only way is to find the DOM element and should set the attribute. I am doing this in app.js.

setTimeout(function () {
  const formElement = document.getElementsByTagName('form')
  formElement.item(0).setAttribute('autocomplete', 'off')
}, 1000)

The above solution is working fine, but if the page loading is slow, then this will not set the attribute. Is there any other way to find the element and set the attribute? I have also tried to set it using the below code after DOMContentLoaded, but it's not working. Nothing inside this event is working.

document.addEventListener('DOMContentLoaded', () => {
   const formElement = document.getElementsByTagName('form')
   formElement.item(0).setAttribute('autocomplete', 'off')
})
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

In React a good way to access DOM elements is by using useRef hook. However, if that element you want to query is not in the JSX of your current component, the useEffect hook could be used with normal JavaScript DOM methods. Like so:

useEffect(()=>{
  const formElement = document.getElementsByTagName('form')
  formElement.item(0).setAttribute('autocomplete', 'off')
},[]);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't find the element because 'DOMContentLoaded' gets fired when HTML file completes loading, not when React renders the form.

How about using MutationObserver instead? https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

Also doing that in root level app.js sounds wrong. Can you do it in the component that is closest to the container of the form?

about 4 years ago · Juan Pablo Isaza 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