1

I have table in my view

Here is code

<tbody id="findings" style="overflow-y: scroll;">
  @foreach (var item in Model) {
  <tr>

    <td style="display: none" id="patientId" class="title">
      @Html.DisplayFor(modelItem => item.Patient_id)
    </td>
    <td class="title appdate">
      @Html.DisplayFor(modelItem => item.Start_appointment)
    </td>
    <td id="nameVal" class="title">
      @Html.DisplayFor(modelItem => item.Patient.Name)
    </td>

    <td class="title">
      <img src="@item.Calculation.CalculationStatus" />
    </td>

    <td class="title">
      <img style="width: 30px; height: 30px;" class="open_calculation" src="~/images/icons8-Document-30.png" />
    </td>
  </tr>
  }
</tbody>

I need to get value from

<td class="title appdate">
  @Html.DisplayFor(modelItem => item.Start_appointment)
</td>

via button click open_calculation

How I can do this correctly via js?

Thank's for help.

4
  • 3
    If you're going to use jQuery, as you've tagged it, then you need these methods: click(), closest(), find(), text() Commented Dec 12, 2017 at 9:58
  • 1
    Like this var date = $(this).find("tr").closest('.appdate').text(); ? @RoryMcCrossan Commented Dec 12, 2017 at 10:00
  • 2
    Yes, exactly like that - just make sure you execute that code in a click event handler. Commented Dec 12, 2017 at 10:02
  • That is not correct, if this is a td. open_calculation you'll need to use something like $(this).parent("tr").find(".appdate").text(). Commented Dec 12, 2017 at 10:08

2 Answers 2

1

Easiest way would be:

$(function(){
    $("body").on("click", ".open_calculation", function(){
        $(this).closest("tr").find(".appdate").text();
    });  
});

Hope this helps!

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

9 Comments

Given the rows are generated in a loop this will get the text of all rows, not the clicked one
It will get it correctly? Because I can have many entries in table?
@OldBalance Updated answer, hope this helps!
Seems it not getting text.
try .html() instead @OldBalance
|
1

So according to @N.Ivanov answer I need to write code like this.

 $('.open_calculation').click(function() {
    openCalculation(this);
});
 function openCalculation(element) {
    $('#main_window').load('@Url.Action("OpenCalculationPartial", "Calculations")',
        function () {
           var date = "Finding from" + " " + $(element).closest("tr").find("#appdate").text().trim();
           $("#appointmentDate").text(date);
        });
}

Where was problem.

I use this in function, but this context in function are different.

So code this code isn't right:

  $('.open_calculation').click(function() {
    openCalculation();
});
 function openCalculation(element) {
    $('#main_window').load('@Url.Action("OpenCalculationPartial", "Calculations")',
        function () {
           var date = "Finding from" + " " + $(element).closest("tr").find("#appdate").text().trim();
           $("#appointmentDate").text(date);
        });
}

2 Comments

You can just use $('.open_calculation').click(openCalculation); and this will be the clicked element.
Yeah. Thank's@Titus

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.