1

I have table that is built dynamically with Razor in MVC.

I want to be able to click on the a row and get the value of lets's say the first column ID.

Right now it's getting all the columns but I don't know too much Javascript or how it's stored with text.

Anyways what I'm looking for is to get a certain column on the row I clicked.

Razor MVC

<table id="myTable" class="tablesorter"> 
<thead> 
<tr> 
    <th>ID</th> 
    <th>Owner</th> 
</tr> 
</thead> 
<tbody> 
@for(int i=0; i<Model.changes.Count(); i++)
{
<tr> 
    <td>@Model.changes[i].ID</td> 
    <td>@Model.changes[i].Owner</td> 
</tr> 
}
</tbody> 
</table> 

Javascript

<script>

    var table = $("#myTable tr");

    table.click(function () {
        alert($(this).text());
    });

</script>

1 Answer 1

1

You can use nth-child,

Live Demo

var table = $("#myTable tr");

table.click(function () {
    alert($(':nth-child(1)',  this).text());
});​
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, this isn't working for me. All the alert shows is :nth-child(1).
Check it now, I just fixed it.

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.