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

162
Visualizações
How to make the buttons work independetly

So I have this table and I have added some buttons inside the table cells

<table border="1">

        <tr>
        <th><button id="0" class="mybtn" >1</button><div class="myDiv"></div></th>
        <th><button id="1" class="mybtn">2</button><div class="myDiv"></div></th>
        <th><button id="2" class="mybtn" >3</button><div class="myDiv"></div></th>
    </tr>
    </table>

When I click on the buttons I want them to add a text field. But when I click on it all three buttons work at the same time.

Here is my jQuery code

$(document).ready(function() {

    $(".mybtn").on("click",function() {

        $(".myDiv").append("<div><br><input type='text' /><br></div>");
    });


});

how to make the buttons work independetly?

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

0

Use parent('th').find('.myDiv').

$(".mybtn").on("click", function() {
  $(this).parent('th').find('.myDiv').append("<div><br><input type='text' /><br></div>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">

  <tr>
    <th><button id="0" class="mybtn">1</button>
      <div class="myDiv"></div>
    </th>
    <th><button id="1" class="mybtn">2</button>
      <div class="myDiv"></div>
    </th>
    <th><button id="2" class="mybtn">3</button>
      <div class="myDiv"></div>
    </th>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

All you have to do is use $(this).next(".myDiv")

$(document).ready(function() {
  $(".mybtn").on("click", function() {
    $(this).next(".myDiv").append("<div><br><input type='text' /><br></div>");
  });
});

Demo

$(document).ready(function() {
  $(".mybtn").on("click", function() {
    $(this).next(".myDiv").append("<div><br><input type='text' /><br></div>");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">

  <tr>
    <th><button id="0" class="mybtn">1</button>
      <div class="myDiv"></div>
    </th>
    <th><button id="1" class="mybtn">2</button>
      <div class="myDiv"></div>
    </th>
    <th><button id="2" class="mybtn">3</button>
      <div class="myDiv"></div>
    </th>
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

Well, you have 3 divs that all have the class "myDiv", so the code gets executed on all three of them if you click any button:

$(".myDiv").append("<div><br><input type='text' /><br></div>");

Either work with IDs or select the parent container and use jQuery $(this) and find() to search for the div with class "myDiv" ONLY in the parent container.

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