Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Javascript: Trigger only parent div content within a duplicated list

I am new to JS. I am learning to apply event listeners, node lists but struggling with the JavaScript syntax for this query. I want to extract the innerHTML of each button to their corresponding textbox within the parent div. So if a user clicks button 3 for example, only textbox 3 value should read the innerHTML of button 3. As you can see, the event listener to trigger each button is working but struggling with the textboxes.

let buttoninput = document.querySelectorAll(".buttoninput");
let textbox = document.querySelectorAll(".textbox");

buttoninput.forEach(btnclickevent => {
    btnclickevent.addEventListener('click', () => {

        textbox.forEach(content => {

            content.value = btnclickevent.innerHTML;
        });
    });
});
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 1</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 2</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 3</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 4</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 5</button>
    </div>

I am aware you can create an id for each textbox input and button and create individual rules but I want to avoid that route as this would be a long line of code. Right now the list is 5 but if I duplicated say 10 or 20 times, I would want the syntax to only trigger each parent div content regardless of the number of duplicates.

Any help would be greatly appreciated.

Thank you

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can find sibling of button by parentNode.children[0] as

btnclickevent.parentNode.children[0].value = btnclickevent.innerHTML;

let buttoninput = document.querySelectorAll(".buttoninput");
let textbox = document.querySelectorAll(".textbox");

buttoninput.forEach(btnclickevent => {
    btnclickevent.addEventListener('click', () => {
        btnclickevent.parentNode.children[0].value = btnclickevent.innerHTML;
        //textbox.forEach(content => {

        //    content.value = btnclickevent.innerHTML;
        //});
    });
});
<div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 1</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 2</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 3</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 4</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 5</button>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use parent check doc property to get the div, the get it children check doc, and edit the textbox. I apologies for not giving a code, but it should work nicely

about 4 years ago · Juan Pablo Isaza Relatório

0

You should use the parentElement to get to the neighbouring input!

let buttoninput = document.querySelectorAll(".buttoninput");

buttoninput.forEach(btnclickevent => {
    btnclickevent.addEventListener('click', () => {
        let textbox = btnclickevent.parentElement.querySelector(".textbox");
        textbox.value = btnclickevent.innerHTML;
    });
});
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 1</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 2</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 3</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 4</button>
    </div>
    <div>
        <input type="text" name="" class="textbox" value="">
        <button class="buttoninput">Button 5</button>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda