My html is like
...
You need to supply a jQuery collection, instead of just a selector to the $.each()
:
$.each($("div[data-role=page]"), function (){
console.log(this.id);
});
Or, even better:
$("div[data-role=page]").each(function (){
console.log(this.id);
});
Please note that I replaced $(this).attr('id')
with this.id
. It gets exactly the same property, but it's way more efficient.
Fiddle Example