0

I have a table with buttons on each row. I am trying to click on a button on a particular and it picks the value .claim_value field of the same row and put the value in the td opposite it .vet_value.

Here's my code

<table id="tasks" class="col-md-12 table">
    <thead>
        <tr>
          <td>Service</td>
          <td>Key notes</td>
          <td>Claim Amount</td>
          <td>Vetted Amount</td>
        </tr>
    </thead>
    <tbody>
        <tr class="therow">
          <td class="claim_value">hey</td>
          <td class="vet_value"></td>
          <button class="button">button</button>
        </tr>
        <tr class="therow">
          <td class="claim_value">you</td>
          <td class="vet_value"></td>
          <button class="button">button</button>
        </tr>
        <tr class="therow">
          <td class="claim_value">me</td>
          <td class="vet_value"></td>
          <button class="button">button</button>
        </tr>
        <tr class="therow">
          <td class="claim_value">them</td>
          <td class="vet_value"></td>
          <button class="button">button</button>
        </tr>
    </tbody>    
</table>

so i want to be able to click on class="button"

$(".button").click(function(){
  (".claim_value").val() of this same row goes into (".vet_value").val()
});

3 Answers 3

1

Just call parent() to access the parent <tr> element and modify <td> from there

$(".button").click(function(){
    var $parent=$(this).parent();
    var claimValue=$parent.find(".claim_value").text();
    $parent.find(".vet_value").text(claimValue);
});
Sign up to request clarification or add additional context in comments.

Comments

0

you don't need to use .val() unless the element is input or textarea

with td use .text() also you need to use $(this) to refer to the button you clicked and closest('tr') to select the closest row then .find() to find the element with class you want

$(".button").click(function(){
     var claim_value = $(this).closest('tr').find(".claim_value").text();
     $(this).closest('tr').find(".vet_value").text(claim_value);
});

Comments

0

Use html() that is not val(). Insert td inside which has to be the direct child of tr. As val() only comes for input and textarea.

$(".button").click(function(){
  alert($(this).closest('tr').find(".claim_value").html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tasks" class="col-md-12 table">
    <thead>
        <tr>
          <td>Service</td>
          <td>Key notes</td>
          <td>Claim Amount</td>
          <td>Vetted Amount</td>
        </tr>
    </thead>
    <tbody>
        <tr class="therow">
          <td class="claim_value">hey</td>
          <td class="vet_value"></td>
          <td>
          <button class="button">button</button>
          </td>
        </tr>
        <tr class="therow">
          <td class="claim_value">you</td>
          <td class="vet_value"></td>
          <td>
          <button class="button">button</button>
          </td>
        </tr>
        <tr class="therow">
          <td class="claim_value">me</td>
          <td class="vet_value"></td>
          <td>
          <button class="button">button</button>
          </td>
        </tr>
        <tr class="therow">
          <td class="claim_value">them</td>
          <td class="vet_value"></td>
          <td>
          <button class="button">button</button>
          </td>
        </tr>
    </tbody>    
</table>

3 Comments

Can we use parent instead of closest? that will work too right?
We can. But the latest jquery uses closest
@kannan * the parent() method traverses to the immediate parent of each of these elements in the DOM tree and constructs a new jQuery object from the matching elements.This method is similar to .parents(), except .parent() only travels a single level up the DOM tree* .. Some think like .find() and .children() .find() find any element in tree but .children() the direct child the DOM tree .. So on this OP code case we can use .parent() cause we need to just travels a single level up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.