I am creating a script that will add in each unordered element of the list, with an exception for the last element of the list - it should end in :. This should happen for every unordered list!
I like...
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ul>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
... at:
<ul>
<li>a,</li>
<li>b,</li>
<li>c;</li>
</ul>
<ul>
<li>d,</li>
<li>e,</li>
<li>f;</li>
</ul>
My code looks like this at the moment ...
$('ul').each(function(i) {
var listsLength = $(this + 'li').length;
$(this + 'li').each(function(j) {
if (i == (listsLength - 1)) {
$(this).append(';');
} else {
$(this).append(',');
}
});
}
I think the problem is in this code ...
$( this + 'li' )
Any idea how to get the same effect?
Edit:
Fixed. There was also a syntax error, and also I used i where j should be placed. Now everything works and here is the result .
Edit # 2:
Just use the @lonesomeday code ... it's very readable and easy!
source
share