My html is like
...
The key reason your code isn't working is because you're trying to iterate a string, which isn't what you really want to do.
To iterate jQuery collections, try the other form of .each()
:
$("div[data-role=page]").each(function() {
//...
});
$.each()
is (usually) for arrays or plain objects.
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
An attribute value must be inside ' '
Try something like this:
$.each("div[data-role='page']", function (){
console.log($(this).attr('id'));
});