Zabs Zabs - 1 year ago 78
jQuery Question

Trying to get selector for img src & 'star rating' column in table w/jQuery

I am trying to run some javascript/jQuery to essentially scrape the data from a table in the format below.
I have got most of this working however I am trying also grab the following data:-

a) The logo column (first column) - the image source e.g '/static/FIFA16/images/crest/50/light/21.png'
b) The 'star rating' column (last column), this doesn't have a number rating but an tag with the format shown below.

I have then getting this data pushed into the result array - can anyone suggest how I grab the Logo (from the src of the image) and the Rating from counting the tags and checking if the last tag has the string 'half' within it?

<table class="table table-striped teams">
<th class="logo">Logo</th>
<th class="name">Name</th>
<th class="league">League</th>
<th class="starrating">Team Rating</th>
<td><a href="/team/21/fc-bayern/" title="FC Bayern"><img alt="FC Bayern" class="team small" src="/static/FIFA16/images/crest/50/light/21.png" title="FC Bayern"/></a></td>
<td data-title="Name"><a href="/team/21/fc-bayern/" title="FC Bayern">FC Bayern</a></td>
<td data-title="League"><a href="/teams/?league=19" title="Bundesliga">Bundesliga</a></td>
<td data-title="Team Rating">
<span class="star">
<i class="fa fa-star fa-lg"></i>
<i class="fa fa-star fa-lg"></i>
<i class="fa fa-star fa-lg"></i>
<i class="fa fa-star fa-lg"></i>
<i class="fa fa-star-half-o fa-lg"></i>
..... more rows...

My jQuery code..

$(".table-striped tr").each( function() {
var $tds = $(this).find('td'),
icon = $tds.eq(0).text()
club = $tds.eq(1).text(),
league = $tds.eq(2).text(),
rating = $tds.eq(3).text,

icon : icon,
club : club,
league : league,
rating : rating
return result;

Answer Source

First, I would recommend to change the selector to $(".table-striped tbody tr"), so that the header isn't loop (since there is no need to).

Then change

icon = $tds.eq(0).text()
rating = $tds.eq(3).text,


icon = $tds.eq(0).find('img').attr('src'),
rating = $tds.eq(3).find('i:first').attr('class');

I thereby assume that rating should result in the class name of the first i. In your example it results in fa fa-star fa-lg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download