jQuery: Closest parent of any type with attribute defined

Using jQuery, how can I find the closest parent with the "dir" attribute defined, regardless of the type of element?

The code below does not work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
  alert('The closest parent of any type with the attribute dir defined has the dir value of ' + $("*[dir]")  );


 <div dir="rtl">
   <p dir="ltr">
       <span id="xyz"></span>


Open in new window

LVL 16
Who is Participating?
David S.Connect With a Mentor Commented:
It only requires a small change to what leakim971 posted in your previous question. I'm surprised he missed it.

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.