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

170
Vistas
Use JS Proxy to get nested form element

I want to make accessing my form elements easier in JS. Forms have the property 'elements' that gives you access to all the DOM inputs. This allows to get elements either by their position or their DOM name.

This becomes cumbersome however when using arrays in the input name:

<input name='shipping[address][city]' />
const shippingCity = form.elements['shipping[address][city]'];

It would be much nicer to access it with dot notation:

const inputs = new Proxy(form.elements, {
  get(target, handler) { 
    ...
  }
}
const shippingCity = inputs.shipping.address.city;

I'm guessing theres a way to do this with JS proxies. I've found examples on how to access nested data in an object but this needs to build the string from the properties instead, then return the DOM at the top level proxy. I'm guessing this would take 2 seperate proxy handlers.

TLDR: Turn X -> Y

inputs.shipping.address.city -> form.elements['shipping[address][city]']
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I can offer you a solution which works like this, but I warn you it's overwhelmingly hacky. It has to wrap every object property with a proxy, and I wouldn't trust it not to create property name collisions which would be horrible to debug.

const subAccessor = (obj, base, path) => new Proxy(obj, {
    get: (target, prop) => {
        const newPath = `${path}[${prop}]`;
        const obj = base[newPath];
        if (target[prop]) return target[prop];
        return subAccessor(obj || {}, base, newPath);
    }
});

const inputs = new Proxy(form.elements, {
    get: (target, prop) => {
        return subAccessor(target[prop] || {}, target, prop);
    }
});

That's code is a truly awful solution, though, so I strongly recommend that you instead use a simple helper function such as

// usage: `getInput('shipping.address.city')`
const getInput = path => form.elements[
    path.split('.')
    .map((part, i) => i > 0 ? `[${part}]` : part)
    .join('')
];
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