Reputation: 10490
I'm really going crazy of this particular obstacle, the issue in hand is that I have a very simple table with rows and all. what I'm trying to accomplish is that when you click on a row then this script is suppose to read the link that's stored in the last td
in the very same row and then direct you there.
What I've come up with so far is this
$('tr td').click(
function (){
alert($(this+':parent td:last-child a:first').attr('href'));
}
);
I've tried 100 of different approaches I either get an error/undefined or I only get the result of the last/first row the rows in-between doesn't work as supposed to.
All help is really appreciated
table looked as following
http://www.jsfiddle.net/xK7Mg/1/
Upvotes: 1
Views: 1373
Reputation: 630389
Use .siblings()
in this case, though .parent().children()
also works, like this:
$('tr td').click(function() {
window.location.href = $(this).siblings('td:last').find('a').attr('href');
});
Take it one step further though, don't attach a click
handler to every cell, use .delegate()
to attach one for the entire table, like this:
$('#tableID').delegate('td', 'click', function() {
window.location.href = $(this).siblings('td:last').find('a').attr('href');
});
Upvotes: 1
Reputation: 35864
$('tr').click(function() {
window.location = $(this).find('td>a').attr('href');
});
Upvotes: 0
Reputation: 5194
$('tr').click(
function (){
window.location = $(this).find("td:last a:first").attr('href');
}
);
Upvotes: 0
Reputation: 50105
What you need is this I think:
$('tr').click(function(){
window.location = $(this).find('td:last a:first').attr('href');
});
This script will cause each table row, when clicked, redirect you to the location referenced in the first anchor element in the last table cell.
Upvotes: 1
Reputation: 163238
I think you meant to do this:
$('tr td').click(function() {
window.location.href = $(this).parent().find('td:last-child a:first').attr('href');
});
Upvotes: 3