Jquery how to get parent name

im looking for an element that provides the name of the parent. It:

$('.Item').click(function(){ var a = $(this).parent(); alert(a[0].tagName); }); 

just says "DIV", but I need the real name Element. Thanks

+8
javascript jquery
source share
7 answers

Try the following (Alerts the tag name and then the Real name):

I used $(a[0]).attr('name');

eg.

 $('.Item').click(function() { var a = $(this).parent(); alert(a[0].nodeName.toLowerCase()); //Tag Name alert($(a[0]).attr('name')); //Attribute (real) name //OR alert(a.attr('name')); //Attribute (real) name }); 
+11
source share

Try:

 var a = $(this).parent().attr('name'); 
+1
source share

Using:

 $('.Item').click(function(){ var a = $(this).parent(); alert(a.attr('name')); }); 
+1
source share

look at this: http://jsfiddle.net/R833t/1/

 $('.Item').click(function(){ var a = $(this).parent(); alert(a.attr('name')); }); 
+1
source share

You can simply use simple Javascript for this - in this case it is even simpler (I think):

 $('.Item').click(function() { var parent = this.parentElement; var tagName = parent.tagName; // tag name, like 'div', 'a', etc... var name = parent.name // value of the 'name' attribute }); 

My source: JavaScript DOM . Take a look at her and help yourself.

Edit: wrapped code inside click event handler.

+1
source share

var a = $ (this) .parent (); notification (a [0] .nodeName.toLowerCase ()); // Tag name

  alert($(a[0]).attr('name')); //Attribute (real) name //OR alert(a.attr('name')); //Attribute (real) name getting issue on this code 
+1
source share

if you want to get the name attribute, you should use .attr () $('.Item').click(function(){ var a = $(this).parent(); alert(a[0].attr("name")); });

0
source share

All Articles