1

I have a table where each row contains several ratings, 5 of which need to be blanked when a certain action is taken and then the original rating put back in the when another action is taken. I've stored the original word in data, so my original line in HTML looks like this.

echo "<td data-Rating = $Rating class = 'outfieldlineuprating'>$Rating</td>";

In my JS, I find all the elements with 'outfieldlineuprating' like so..

SkillRatings = PlayerRow.find('.outfieldlineuprating');

It works up to this point (PlayerRow is fine). I know it works because when taking the first action (i.e blanking the elements), it works with ..

SkillRatings.html('----');

This puts ---- in all of the 5 elements with the 'outfieldlineuprating' class. Reversing it however, I'm struggling with. 'SkillRatings' at this point presumably contains some sort of array of the relevant td's, but I don't know how to loop through it, extract the data and insert that into the html. Here's what I'm trying but this doesn't work in JS.

for (var index = 0; index < SkillRatings.length; index++){
    Rating = SkillRatings[index].attr('data-Rating');
    SkillRatings[index].html(Rating);
}

This just gives the error : SkillRatings[index].attr is not a function

4
  • 1
    This is also a Jquery question. Can you add that tag as well? Commented Jun 28, 2016 at 16:13
  • 1
    Looks your code use jQuery, if is so try: $(SkillRatings[index]).attr. Commented Jun 28, 2016 at 16:14
  • Oops yeah, it's JQuery. Errr, I'll try to add the tag. Commented Jun 28, 2016 at 16:16
  • Also thanks Mario, that fixed it. I need to swat up on the syntax of JQuery because I had it right apart from the syntax. Commented Jun 28, 2016 at 16:18

1 Answer 1

3

You are using jQuery and SkillRatings = PlayerRow.find('.outfieldlineuprating'); creates a jQuery object containing the elements.

SkillRatings[index] returns an actual element, not the jQuery object for that element

You would need to change SkillRatings[index].jQueryMethod() to

SkillRatings.eq(index).jQueryMethod(); 
// or 
$(SkillRatings[index]).jQueryMethod();

However this is easier done using SkillRatings.each which exposes this as the current element in the loop

Instead of using for loop try

SkillRatings.each(function(){
     // "this" is current element
     $(this).html( $(this).data('Rating'));
});
Sign up to request clarification or add additional context in comments.

2 Comments

OK...there is a mismatch in question then between data-RatingData and data-Rating shown in your html.
Yes you're right. Have updated question. Your answer is correct and the loop works. Thanks :)

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.