i want know how select particular td in table.
$.each(result.hotelsearchresult.hotelresults, function (index, item) { var imgpath = item.hotelpicture; var rate = item.starrating; var table = $('#tableid'); var rows = '<tr><td rowspan="4" width="16.666%"><img src="' + imgpath + '" />'+ "</td>"; rows += '<tr><td class="code">' + addscore(rate, $("td.code")) + "</td></tr>"; table.append(rows); });
here, addscore
function , requires element second parameter append stars according rating. following this post, in code appending ratings particular td
each iteration. means if there 20 arrays appending 20 ratings each td
in table. want append 1 rating per td
.
this table:
<div id="divresult5"></div> <table> <tr></tr> <tbody id="tableid" style="border:solid 1px red;"></tbody> </table>
please tell me how should done.
you can modify addscore below , try:
function addscore(score) { var classname = 'stars-container stars-'+score.tostring(); return "<span class='"+classname+"'>★★★★★</span>"; }
and invoke addscore below:
rows += '<tr><td class="code">' + addscore(rate) + '</td></tr>';
here reason why original function not working: original function expecting dom element second argument, , appends score span element dom element.
in case when sending $("td.code") there 2 problems associated it: selector "td.code" select td-elements class ".code" means behaves below :
=> when iterates first row, not find td element because current td element not yet in dom.
=> when iterates second row, not find current td element find td element of first row because first row td-element having same class ".code"
=> when iterates nth row, not find current td nth element find td elements of (n-1) rows
so current td element trying append stars not yet in dom, cannot pass second argument.
instead need function generate html markup (span stars) , can directly invoke function, addscore(score) return markup.