I am using jquery and the each function but I am having trouble finding the last item in a ul list.
DATA:
<ul id="1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul id="2">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul id="3">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
I have tried multiple things but nothing is doing what I want. Here is I have tried.
var test = '{';
for (i = 1; i <= 3; i++) {
test += i+':{';
$('#'+i+' li').each(function(index,e){
test += $(this).attr('id');
// THIS IS THE PROBLEM AREA START
if(index != $('#'+i).last()){
test += ',';
}
// PROBLEM AREA END
});
test += '}';
if(i != 3){
test += ',';
}
}
test += '}';
alert(test);
I have also tried using the "$('#'+i+':last')" but that did not do it ether.
The output I am trying to get is:
{1:{1,2,3},2:{1,2,3},3:{1,2,3}}