alhashmiya
alhashmiya

Reputation: 179

Get a href from specific table cell

Dear experts I have following Table Structure

<table>
<tr>
<td colspan="2">short description</td>
</tr>
<tr>
<td><a href="Disclose.html">View</a></td>
<td><a class="AgendaNote" href="#">Take Note</a></td>
</tr>
</table>

What I want on click class AgendaNote I need extract the href attribute of first td that is "Disclose.html"

I tried

alert($(this).parent().siblings(":first > a").attr("href"));

but it showing undefined.

Please help me.

Upvotes: 2

Views: 4246

Answers (2)

Ram
Ram

Reputation: 144659

siblings method doesn't work that way, the target a element is not the sibling of the parent td element so the query fails and attr returns an undefined value, you should at first select the sibling td element and then select the child element.

$(this).parent().siblings(":first").children('a').attr("href");

But better option here is using the prev method:

$(this).parent().prev().children().attr("href");

Upvotes: 5

Arun P Johny
Arun P Johny

Reputation: 388316

The target a is in the previous sibling of the current td so try

alert($(this).parent().prev().find('a').attr("href"));

Demo: Fiddle

Upvotes: 2

Related Questions