Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!