Map parents to get its name in jQuery

Description

The following code shows how to map parents to get its name.

Example


<!--from   w w  w.j a va  2  s.  c  o  m-->
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var parentEls = $("span").parents()
.map(function () {
return this.tagName;
})
.get().join(", ");
$("span").append(parentEls);
});
</script>
</head>
<body>
<body>
<div>
<p>
<span>
</span>
</p>
</div>
</body>
</html>

Click to view the demo

The code above generates the following result.

Map parents to get its name in jQuery
Home »
  Javascript Tutorial »
    jQuery »
      jQuery DOM
...
Create a paragraph clone then add a hard co...
Get HTML content in jQuery
Get image src attribute value in jQuery
Get text and attribute in jQuery
Get text from first paragraph and set it as...
Get the content of a paragraph and convert ...
Get the title attribute from an image eleme...
Insert a cloned element after selected elem...
Insert a hardcoded element after in jQuery
Insert a selected element after in jQuery
Insert a selected element with prepend in j...
Insert hard coded HTML element after select...
Insert hardcoded HTML after all paragraphs ...
Insert hardcoded element to a span element ...
Insert selected content before each of the ...
Insert text node created from document.crea...
Join the tag name with map function in jQue...
Map each list item to uppercase in jQuery
Map parents to get its name in jQuery
Map value with index and value in jQuery
Remove an attribute by name in jQuery
Remove element from selected list by its st...
Remove element from the selected list by id...
Remove just added in hover event in jQuery
Remove select elements which contains a cer...
Replace all in click event for clicked elem...
Replace all paragraphs with hard coded elem...
Replace clicked element and keep its text i...
Replace with hard coded tag in jQuery
Reverse of $(A).before(B) with .insertBefor...
Reverse the $(A).after(B) with $(A).insertA...
Reverse the result of $(A).append(B) in jQu...
Set HTML tag as text in jQuery
Set Image URL, title and alt by passing in ...
Set attribute value with attr and a functio...
Set div element text value in jQuery
Set harcoded HTML content in jQuery
Set html content with function to create co...
...