1

I have a jquery datatable that contains multiple columns and rows as below.

<table>
    <thead>
        <tr>
            <td>Name</td>
            <td>Class</td>
            <td>Action</td>
            <td>Score</td>
            <td>Corrected Score</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Student1</td>
            <td>Math</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>73</td>
            <td>
                <input type="text" id="1_final_score" />
            </td>
        </tr>
        <tr>
            <td>Student2</td>
            <td>Biology</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>84</td>
            <td>
                <input type="text" id="2_final_score" />
            </td>
        </tr>
        <tr>
            <td>Student3</td>
            <td>History</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>50</td>
            <td>
                <input type="text" id="3_final_score" />
            </td>
        </tr>
    </tbody>

When i click on the "Add 5" link, I want to add 5 to the score in the corresponding row and insert the result in the final score input field. I have tried using the jquery row-select feature, but am unable to figure out how to accomplish the above.

1

3 Answers 3

4
$(document).ready(function () {
$('table').find('a').click(function () {
    var original = parseInt($(this).parents('tr').find('td:nth-child(4)').text()) + 5;

    $(this).parents('tr').find('input[type=text]').val(original);

   });

});
Sign up to request clarification or add additional context in comments.

2 Comments

$('table') should be $('#table')
It is html tag not id so $('table')
1

You can try this:

$(document).ready(function(){
    $("a").on("click", function(){
        var num=parseInt($(this).closest("td").next("td").text());
        num=num+5;
        $(this).closest("td").next("td").text(num);
        $(this).closest("tr").find("input").val(num);

    });
});

FIDDLE

Comments

0

Try with this

$('a:contains("Add 5")').click(function(){
    $(this).parents('tr').find('td input:text').val(+$(this).parent().next('td').text() + 5 )
 });

FIddle : http://jsfiddle.net/2n0bevxo/172/

Comments

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.