[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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">
<head>
<title>Hello</title>
<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]")  );
});
</script>

</head>
<body>

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

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
1 Solution
 
David S.Commented:
It only requires a small change to what leakim971 posted in your previous question. I'm surprised he missed it.
$("#xyz").closest("[dir]").attr("dir")

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now