6

Possible Duplicate:
How to find the nearest common ancestors of two or more nodes?

I have 2 anchor elements and i need to get the common parent of 2 of them.

Some times they are in a ul, sometimes in a table, or any other way.

I am doing this:

var idParent = $(elem1).parents().filter($(elem2).parents()).first().attr('id');

But it doesnt always work, i finds it element sometimes...

Any better idea? or a way to fix this?

EDIT: I want a jquery solution.

Thank you

Community
  • 1
  • 1
Ovi
  • 2,459
  • 9
  • 50
  • 72

1 Answers1

21
var parent = $(elem1).parents().has(elem2).first();
Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68