I think the contents of this could just be `$("a", listItems[index]).toggleClass("checked", checked);` (there's no harm in trying to remove the class if it doesn't have it).