Thu marlo
Thu marlo

Reputation: 553

jquery how to get Name of parent

im searching for the Element whicht Provides the Name of a Parent. This:

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

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

Upvotes: 10

Views: 36296

Answers (7)

Amit Kumar
Amit Kumar

Reputation: 11

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 getting issue on this code 

Upvotes: 1

Daan Wilmer
Daan Wilmer

Reputation: 955

You could just use plain Javascript for this - in this case it's 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 it, and help yourself.

Edit: wrapped code inside the click event handler.

Upvotes: 1

Marc Uberstein
Marc Uberstein

Reputation: 12541

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

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

e.g.

$('.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
    });

Upvotes: 13

kleinohad
kleinohad

Reputation: 5912

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

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

Upvotes: 1

Neo
Neo

Reputation: 121

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

Upvotes: 0

Darm
Darm

Reputation: 5659

Use:

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

Upvotes: 1

rolling stone
rolling stone

Reputation: 13016

Try:

var a = $(this).parent().attr('name');

Upvotes: 1

Related Questions