2

I am trying to use an each() function and every time the class appears in the table it gets the information within that section.

It is doing the loop x3 but the information i am asking for is only getting from the first instance.

<td class="approved_player"> 
<span first_role="1" role="Damage" class="icon_damage"></span>   
<a class="class_monk">Sickli</a>
</td>

After it has read the information it updates the json array 1 in either damage or not.

var classes = {
    classes: [{
        "mage": 0
    }, {
        "warlock": 0
    }, {
        "rogue": 0
    }, {
        "paladin": 0
    }, {
        "deathknight": 0,
            "deathknight damage": 0
    }, {
        "monk": 0,
            "monk damage": 0
    }, {
        "priest": 0,
            "priest damage": 0
    }, {
        "druid": 0,
            "druid damage": 0
    }, {
        "shaman": 0,
            "shaman damage": 0
    }]
};

$("td.approved_player").each(function (i, obj) {
    var player = $("a[class^='accountLink class_']").attr('class').split('_')[1];
    var role = $("span[first_role='1']").attr('role').toLowerCase();

    $.each(classes, function (key, data) {
        $.each(data, function (index, data) {
            if (player == data) {
                if (role == 'damage') {
                    data[1] = 1;
                } else {
                    data[0] = 1;
                }
            }
        });
    });

    $("div#output").append(i + ' appoved class ' + player + ' role ' + role + '<br/>');

});


    $.each(classes, function (key, data) {
        $.each(data, function (index, data) {
            console.log('index', data);
        });
    });

I am working on this here: http://jsfiddle.net/EXwsy/1/

Thanks for any help.

1 Answer 1

1

Well i found an error

Here you're comparing you data object with the string of the player

 if (player == data) 

I changed this to:

if (Object.keys(data)[0] == player)

In this way you will compare the "monk" with the first property of your object in this case is the mage one.

And also this will retrieve 3 elements

  $("td.approved_player")

But inside your each loop you get an element like this but it will retrieve 3 elements

$("a[class^='accountLink class_']")

This will also retrieve 3 elements and you work always on the first.

Using this you will retrieve the current element.

$(this).children("a").attr('class').split('_')[1]

Here is the working fiddle well i think so.

Just remember that jQuery can retrieve multiple DOM objects from a desired selector.

Sign up to request clarification or add additional context in comments.

1 Comment

Yw it was a pleasure to help out in code like this, for a gamer xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.