When you use $('#sortable li').attr('id')
jQuery will return only the id
of the first element that this selector finds.
When you use that line of code inside a .each()
you’re not actually changing anything, and that’s why he gives the same answer both times.
However the selector $('#sortable li')
returns all the elements you want and it makes sense to use the .each()
to iterate them. Since jQuery assigns the iterated element to this
within that function passed to .each()
then to know the id
just use this.id
, as I mentioned in my comment earlier.
So just use it:
$('#sortable li').each(function(){
console.log(this.id);
});
NOTE: Notice you’re missing });
at the end of your code, before the </script>
.
Can you join HTML? so we can respond more pedagogically. Briefly you are always using the same element... you should use
console.log(this.id);
. But if you join HTML it’s easier to understand the problem and answer.– Sergio