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

279
Vistas
Can I target a specific Flexbox container and fill it with the contents of another file?

I am looking for a way to dynamically populate a submenu depending on the selection of the main menu, then, when a user clicks on an item in the submenu, it populates two flexboxes with the contents of another file. I can't figure out how to target a flexbox using JS; or, if that's not possible, what I could do instead. For example:

    MENU 1     MENU 2     MENU 3     // user selects menu 2, which populates the submenu from a file
                 ^
    submenu 1 submenu 2 submenu 3    // user selects submenu 3, which populates the flexbox containers

FLEXBOX CONTAINERS:

------------------------  -----------------------------------------
| SUBMENU 3 HTML PAGE  |  |        SUBMENU 3 HTML PAGE            |
|                      |  |                                       |
|                      |  |                                       |
|   has options that   |  |       dynamically populates           |
|  affect the contents |  |      based on the options selected    |
|   of the other box   |  |       in the other box                |
|                      |  |                                       |
------------------------  -----------------------------------------

Is this possible? What should I search to figure it out? I have Googled to no avail, I'm not searching for the right phrase. What should I be looking for?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is one possible implementation. You add an event listener on each trigger element—in my case, a button. When the button is clicked, you target the .flex element adjacent to the button and dynamically insert HTML content.

const btns = Array.from(document.querySelectorAll('button'))

const clearContent = () => {
  Array.from(document.querySelectorAll('.flex')).forEach(item => item.innerHTML = '')
}

btns.forEach((btn, index) => {
  btn.addEventListener("click", () => {
    clearContent();
    btn.parentNode.querySelector('.flex').innerHTML = menuContents[index]
  })
})

const menuContents = [
  '<div>sub 1</div><div>sub 2</div><div>sub 3</div>',
  '<div>sub 4</div><div>sub 5</div><div>sub 6</div>',
  '<div>sub 7</div><div>sub 8</div><div>sub 9</div>'
]
.flex {
  text-align: center;
  display: flex;
  position: absolute;
  left: 50%;
  top: 1.5rem;
  transform: translateX(-50%);
  column-gap: 1rem;
  white-space: nowrap;
}

.flex:empty {
  display: none;
}

.outer {
  position: relative;
}

body {
  display: flex;
  justify-content: center;
  gap: 1rem;
}
<div class="outer">
  <button>
    Menu 1
  </button>
  <div class="flex"></div>
</div>

<div class="outer">
  <button>
    Menu 2
  </button>
  <div class="flex"></div>
</div>

<div class="outer">
  <button>
    Menu 3
  </button>
  <div class="flex"></div>
</div>

jsFiddle

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