How "$(selector).each()" function is working in jquery?
Hi
Here is the code :
<!DOCTYPE html>
<html>
<script src="jquery-1.11.0.min.js">
</script>
<body>
<table id = "myTable" style="width:300px">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>5</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>5</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>11</td>
</tr>
</table>
<script>
$(document).ready(function(){
$('td:last-child').each(function(i) {
$(this).html((function A(n) {if (n==1)
return 1;
else
return n*A(n-1)})( $(this).html() ));
})
})
/script>
</body>
</html>
Here I am printing factorial of numbers mentioned in table and above code is working fine.But If I used the follwing code :
$('td:last-child').each(function(i,value) {
$(this).html((function A(n) {if (n==1)
return 1;
else
return n*A(n-1)})( value ));
})
if I am not mistaken here "value" refers to (5,5,11) at each iteration so why it is printing the fctorial of just last number .The code should behave in the same way as the first approach that I have mentioned.but it is not.
Here "function(i,value)" following the same syntax as in this jsfiddle "http://jsfiddle.net/9C8He/".
Please suggest me the solution in second case.
Thanks
Varun