We help IT Professionals succeed at work.

jQuery: Closest parent of any type with attribute defined

hankknight asked
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

Watch Question

Consultant & Challenge Subduer
Top Expert 2009
It only requires a small change to what leakim971 posted in your previous question. I'm surprised he missed it.

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.