New to jQuery and I don't know how else to say it.
When the function below is within the jquery selection, it works fine. When I try to break out the function I'm not sure what object/variable needs to be passed into the function in order to make the function work.
//function included in selection works great
$('#id1 tbody tr').find('td:eq(2)').text(
function(i,t){
return Math.round((parseInt(t,10) / 60)*100)/100;
});
//function broken out returns NaN in HTML
$('#id2 tbody tr').find('td:eq(2)').text(minutesToHours());
function minutesToHours(i,t){
return Math.round((parseInt(t,10) / 60)*100)/100;
}
It's the same function but returns 'NaN' in the HTML. I know I need to pass the object into the function minutesToHours()
, but nothing I've tried works-- this
or $(this)
. minutesToHours('whatGoes','here')
I'm assuming this has something to do with context but I can't figure it out.
Example HTML
<table id="id1">
<tbody>
<tr>
<td>John</td>
<td>2</td>
<td>800</td>
<td>4</td>
</tr>
<tr>
<td>Sally</td>
<td>5</td>
<td>610</td>
<td>9</td>
</tr>
<tr>
<td>Bob</td>
<td>7</td>
<td>249</td>
<td>3</td>
</tr>
</tbody>
</table>
<table id="id2" style="margin-top:1em;">
<tbody>
<tr>
<td>John</td>
<td>2</td>
<td>800</td>
<td>4</td>
</tr>
<tr>
<td>Sally</td>
<td>5</td>
<td>610</td>
<td>9</td>
</tr>
<tr>
<td>Bob</td>
<td>7</td>
<td>249</td>
<td>3</td>
</tr>
</tbody>
</table>
Result
Included function
John 2 13.33 4
Sally 5 10.17 9
Bob 7 4.15 3
Broken out function
John 2 NaN 4
Sally 5 NaN 9
Bob 7 NaN 3
Thanks for your help!