3

I have the following Delete confirmation code using jquery. The issue is that it assume the location of the name column. I now have name in the first row. Instead of using .prev('td'), is there anyway to get the value from the first column in the current row using jQuery?

<script type='text/javascript'>
    $(document).ready(function() {
        $("a.delete").click(function(e) {
            e.preventDefault();
            var url = $(this).attr("href");

            var name = $(this).parent().prev('td').prev('td').text();

            jConfirm('Are you sure you want to delete this:' + name, 'Application Delete', function(r) {
                if (r) {
                    window.location = url;
                }
            });
        });
    });
</script>
1

1 Answer 1

9

You could use under each

var name = $(this).parents('tr:first').find('td:first').text();

this goes to the row and then to the first cell of the row. That's more intuitive.

Edit: I changed 'tr' to 'tr:first', but in real this should often not be needed, because nested tables more than often indicate a different problem in the whole design (tables for layout?) which needs to be fixed differently.

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

3 Comments

hmm .. this seems to give me a string that represents the whole table heading . .any ideas what could be wrong ?
Apparently you're nesting tables (why by the way?). Replace tr by tr:first.
yes, this is a nested table . . basically for layout purposes as their is a main table that represents the whole page and this is a section within the main page . .

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.