1

I'm trying to capture the id attribute on the parent here, and It returns as undefined

<table>
  <tr id="1343">
    <td><span class="hi">Hi</span></td>
  </tr>
</table>

Javascript:

var $id = $(".hi").parent("tr").attr("id");
alert($id); 

I thought I was using it right, but I guess not, anyone know why it won't give me the value I'm trying to get?

Here's a fiddle: http://jsfiddle.net/AsDY6/

Control Freak
  • 12,965
  • 30
  • 94
  • 145

6 Answers6

3

You can use parents instead of parent on your row selector.

var $id = $(".hi").parents("tr").attr("id");

As noted by others closest will also work. The difference is that parents will get all parent elements that match the selector. So if you had nested tables you would probably want to use closest because parents would get all parent table rows. If I know the DOM will always only have one parent matching the selector, I tend to prefer parents for performance reasons because parents is faster according to performance tests like this one.

Here's an updated fiddle.

clav
  • 4,221
  • 30
  • 43
  • 1
    As this test indicates (http://jsperf.com/jquery-parents-vs-closest/32) it's also faster to use `.parents()` than it is to use `.parents().first()` and both are faster than `.closest()` -- a bit of a surprise. I will edit my answer accordingly. – Derek Henderson May 07 '13 at 16:19
3

.parent() returns the immediate parent, which in this case is the td

Use closest instead. $id = $(".hi").closest("tr").attr("id");

Trenton Trama
  • 4,890
  • 1
  • 22
  • 27
1

Try .closest('tr') or .parents('tr') instead of .parent('tr');.

.parent() will only go up one level, whereas .closest() will continue to climb up the DOM until it finds a match and .parents() will search up to the root ancestor. (You can also chain .first() to .parents() (e.g., .parents().first()) so that it returns only the first match.)

See Difference between jQuery parent(), parents() and closest() functions for more information about the different methods. And check out these performance comparisons between the three proposed answers.

Community
  • 1
  • 1
Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
1

Change

var $id = $(".hi").parent("tr").attr("id");

TOO

var $id = $(".hi").parents("tr").first().attr("id");

OR

var $id = $(".hi").closest("tr").attr("id");

WHY

The answer is simple. The tag <tr> is not the parent of .hi. .hi's parent is actually the <td> it resides in. Thus you can use .parents instead. However, this could grab one too many, so make sure to use something like .first if you only want the First parent fitting that description.

OR

Use jQuery's .closest method which is supposed to do about the same thing as .parents('selector').first()

SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
-1

Reference parent of parent

Use this code:

var $id = $(".hi").parent().parent().attr("id");

It's work!

  • If the class "hi" gets added to a different element, one that isn't a child of the TD, your code breaks, so it is not a very robust solution. – Derek Henderson May 07 '13 at 16:04
-2
   var $id = $(".hi").parent("td").parent('tr').attr("id");
  alert($id); 
Ahmed Alnahas
  • 263
  • 1
  • 4