Reputation: 5298
I am trying to find a child element within a table row. The code is in the below link. I am not able to get to find an anchor tag within a table row. What Am I doing wrong here?
<a href="##" class="clickevent">Click Here</a>
<table>
<tr id="test_tr">
<td>
<a href="##" class="secondlink">Add New Row</a>
</td>
</tr>
<table>
JS:
$(document).ready(function() {
$(".clickevent").click(function(e){
e.preventDefault();
alert($("#test_tr").find(".secondlink").tagName);
});
});
Upvotes: 2
Views: 16627
Reputation: 17077
Use $("#test_tr").find(".secondlink")[0]
to access the DOM element associated with the jQuery Object. Using [1]
would point to the 2nd matched DOM element, and so on...
Upvotes: 2
Reputation: 11
Here is what I did to implement "double-clicking on rows" behavior on my table, which then simply follows an anchors href that is embedded in one of the cells in each row.
$("#table_id tr").dblclick(function(e){
if(e.target.parentNode.rowIndex){
window.location = $("#table_id tbody tr:eq("+(e.target.parentNode.rowIndex-1)+") a:last").attr(href);
}
});
Notice the "a:last" part, this will grab the last anchor element in the table row. You can also make this "a:first" and grab the first anchor element in that table row.
Upvotes: 1
Reputation: 7935
Here you go, .find() returns a list of elements, you need to get the first one.
$("#test_tr").find(".secondlink")[0].tagName
Upvotes: 0