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

183
Views
How to make the row number to adjust automatically when drag and drop a row in HTML

There is a HTML table which is set tbody to draggable via jQuery.

$('tbody').sortable();
                            
   var checklistContentTableOnSort = document.getElementById('table');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table class="table table-bordered table-hover" id="table">
   <thead>
      <tr>
         <th class="text-center"> # </th>
         <th class="text-center"> Text </th>
         <th class="text-center"> Comment </th>
      </tr>
   </thead>
   <tbody class="ui-sortable">
      <tr id="addr0" class="ui-sortable-handle">
         <td> 1 </td>
         <td>         <input type="text" name="text0" placeholder="Text" class="form-control" onchange="addValueOnChange(this)" value="Adg">       </td>
         <td>         <input type="text" name="comment0" placeholder="Comment" class="form-control" onchange="addValueOnChange(this)" value="A">       </td>
      </tr>
      <tr class="ui-sortable-handle">
         <td>2</td>
         <td><input name="text2" type="text" placeholder="Text" class="form-control input-md" onchange="addValueOnChange(this)" value="B"> </td>
         <td><input name="comment2" type="text" placeholder="Comment" class="form-control input-md" onchange="addValueOnChange(this)" value="B"></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td>3</td>
         <td><input name="text3" type="text" placeholder="Text" class="form-control input-md" onchange="addValueOnChange(this)" value="C"> </td>
         <td><input name="comment3" type="text" placeholder="Comment" class="form-control input-md" onchange="addValueOnChange(this)" value="C"></td>
      </tr>
   </tbody>
</table>

In here the first column has the row number. When dragging a row, I want to re arrange all the number to the correct order. can you suggest me a way to do this via JavaScript or jQuery.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Personally I would be tempted to use a CSS counter variable and assign that as the content of the table cell so then it should not matter how these are dragged it will be automagically be numbered. Id also be tempted to remove the inline event handlers in favour of an externally registered version.

const addValueOnChange=(n)=>{alert(n.value)}// example only

document.querySelectorAll('tbody td input[type="text"]').forEach(n=>n.addEventListener('change',function(e){
  addValueOnChange(this)
}))
:root{
  counter-reset:rows;
}
tbody tr{
  counter-increment:rows;
}
tbody tr td:first-of-type:before{
  content:counter(rows);
}
<table class="table table-bordered table-hover" id="table">
   <thead>
      <tr>
         <th class="text-center"> # </th>
         <th class="text-center"> Text </th>
         <th class="text-center"> Comment </th>
      </tr>
   </thead>
   <tbody class="ui-sortable">
      <tr id="addr0" class="ui-sortable-handle">
         <td></td>
         <td><input type="text" name="text0" placeholder="Text" class="form-control" value="Adg" /></td>
         <td><input type="text" name="comment0" placeholder="Comment" class="form-control" value="A" /></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td></td>
         <td><input name="text2" type="text" placeholder="Text" class="form-control input-md" value="B" /></td>
         <td><input name="comment2" type="text" placeholder="Comment" class="form-control input-md" value="B" /></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td></td>
         <td><input name="text3" type="text" placeholder="Text" class="form-control input-md" value="C" /> </td>
         <td><input name="comment3" type="text" placeholder="Comment" class="form-control input-md" value="C" /></td>
      </tr>
   </tbody>
</table>

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!