DG3
DG3

Reputation: 5298

Finding the child element (anchor tag) within a table row

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?

http://jsfiddle.net/vXwNJ/2/

<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

Answers (5)

BMiner
BMiner

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

Eric
Eric

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

Nican
Nican

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

dmackerman
dmackerman

Reputation: 2966

If you want to get the "content" of the link, use .html()

http://jsfiddle.net/vXwNJ/8/

Upvotes: 0

mattsven
mattsven

Reputation: 23283

You have to remember that there is a difference between jQuery Element objects and actual Elements.

alert($("#test_tr").find(".secondlink")[0].tagName);

Doing [0] works because jQuery is also an array of DOM Element objects, there is also $.get.

Upvotes: 5

Related Questions