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

258
Visualizações
JQuery: How to select a specific input wrapped in a TD inside a table

I have a table, whose rows and td's are created with a foreach. So there is no predicting how many rows there might be. Each row has two buttons accompanied by an two adjacent input fields. See image:Table Image When you click a button, I need to grab the value inside it's adjacent input which is wrapped in a td. I have tried everything: prev(), closest(), eg(). And I can not figure out how to isolate the specific input on click. The best I have achieved is applying some css to ALL the inputs, but not the specific partnered input.

HTML

<tbody>
                @foreach($registeredChildren as $child)
                <tr>
                    <td class="col-2"><a href="'/getchild/'+ {{$child->child_id}}">{{$child->childFirstName}}</a>&nbsp &nbsp {{$child->childLastName}}</td>

                    <!--========TIME IN===========-->
                    <td class="col-3 pb-2 timeIn">
                        <input id="{{$child->child_id}}" class=" form-control " value="{{$child->timeIn}}" style="text-align:center;"  type="text">
                    </td>

                    <td><button class="btn btn-outline-success timeInReset">Reset</button></td>

                    <!-- //========TIME Out ===========//-->
                    <td class="col-3 pb-2"><input style="text-align:center;" class="form-control " type="text" ></td>
                    <td><button id="timeOut/{{$child->child_id}}" class="btn btn-outline-success out">Reset</button></td>
                </tr>
                @endforeach
           </tbody>

Script - This Does Not Work!

$(document).ready(function(){

$("button.timeInReset").click( function () {

 $(this).closest("td.timeIn>input").css({"color": "red", "border": "2px solid red"});

});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

closest doesn't look up sibling elements, but from current level up the DOM tree for parents.

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree

$("button.timeInReset").click( function () {
    $(this).closest("tr").find("td.timeIn>input").css({"color": "red", "border": "2px solid red"});
});
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