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

124
Views
closest tr selection in JQuery select dropdown

Hope someone can advise.

I'm facing small issue trying to add certain data into a select dropdown which is <option> elements that I'm fetching it with Ajax after clicking the check box in same td in a table.

Html:

<td class="col-lg-4"><select name="name[]" class="form-control name"></select>

Now the JS:

$(document).on('click', '.check_box', function(){
            var name = $(this).data('name');
            $.ajax({
                type: 'POST',
                url: '/someactionhere/',
                data:{name:name},
                success: function(data){
                    $(this).closest('tr').find('.name').html(data);
                }
            });

});

I know it's should be real simple but and looks like right to me. So I don't no if something wrong or not.

Any ideas?

Thanks in advance.

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

0

In your ajax function, $(this) refers to the ajax function object, not the element that was clicked. To refer to that element, you need to first store it in a variable, like the comments suggest.

$(document).on('click', '.check_box', function() {
    var name = $(this).data('name');
    let elem = $(this)
    $.ajax({
        method: 'GET',
        url: 'https://jsonplaceholder.typicode.com/todos/1',
        data: {
            name: name
        },
        success: function(data) {
            elem.closest('tr').find('.name').html("<option>here is the html</option>");
        }
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <td class="col-lg-4">
      <input type='checkbox' data-name='test' class='check_box' />
      <select name="name[]" class="form-control name">
      </select>
    </td>
  </tr>
</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!