I have used the hasClass method before but I have a scenario where is reporting as not been present although if I output the elements .html() it clearly shows the class is in use by item aswell as other classes.
The element that the class belongs too was dynamically written to the screen. Any ideas what I’m doing wrong?
$('.myList').each(function (index, item) {
// Check marked as complete
if (!$(item).hasClass('complete')) {
// Not complete
alert('not complete!' + $(item).html());
completeFlag = false;
return false;
}
});
The html output:
<li id="myid" class="class1 class2 complete">some text</li>
I’m guessing you’re checking the list, where you mean to be checking the list items.
Example of what I think you’re doing:
HTML:
JavaScript:
Live copy (which doesn’t work)
Here’s how you’d check the items: Change
to:
Live copy