Youss
Youss

Reputation: 4222

How to attr class

I have a piece of code which someone wrote for me like this:

var $me = $('.me'),
        crazyNumber = 99999999,
        allOfYou = [
            { 
                elements: $me.parentsUntil('.you'),
                target: $me.parentsUntil('.you').parent()
            },
            { 
                elements: $me.nextUntil('.you'),
                target: $me.nextUntil('.you').andSelf().filter(':last').next()
            },
            { 
                elements: $me.prevUntil('.you'),
                target: $me.prevUntil('.you').andSelf().filter(':last').prev()
            }
        ],
        sorted = allOfYou.sort(function (objA, objB) {
            var a = objA.elements.length + 1 || crazyNumber,
                b = objB.elements.length + 1 || crazyNumber;
            return a - b;
        });

    $('#who').html('you ' + sorted[0].target.data('verb') + ' me');

In the end the div#who gets html from target.data('verb') I would like to get the class instead but I dont understand the code, I tried:

$('#who').html(sorted[0].target.data.class);
$('#who').html(sorted[0].attr('class'));
$('#who').html(sorted[0] + $(this).attr('class'));

And many other things, nothing seems to work (Im not familiar with Jquery target..)

Example at JsFiddle

Upvotes: 0

Views: 1806

Answers (3)

palaѕн
palaѕн

Reputation: 73926

You were near, replace your code this:

$('#who').html(sorted[0].attr('class'));

with this:

$('#who').html(sorted[0].target.attr('class'));

Upvotes: 1

jgillich
jgillich

Reputation: 76289

The jQuery object is saved into target.

sorted[0].target.attr("class");

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

Probably, you are looking for this -

$('#who').html(sorted[0].target.attr('class'));

http://jsfiddle.net/mohammadAdil/FyqsW/3/

Upvotes: 3

Related Questions