In my HTML there are number of divs each having names inside. I have to implement alphabetic filter in it. If user clicks on button 'A-J' the list will make only divs where first letter of name is between A-J and similarly for other letter groups. I have written following piece of code till now :
$("#jfmfs-filter-selected-aj").click(function(event) {
event.preventDefault();
for (i = 0; i < all_friends.length ; i++)
{
var aFriend = $( all_friends[i] );
if(!(/^[a-jA-J]+$/.test(aFriend.find(".friend-name").html().charAt(0)))){
aFriend.addClass("hide-filtered");
}
}
$(".filter-link").removeClass("selected");
$(this).addClass("selected");
});
This Code works fine but hides div in a loop and hence it takes time. Is there a way i can write the above code in single line to add "hide-filtered" class to all divs which meets my criteria in one go Something like this?
all_friends.not( {divs with inner html starting with letters a-j} ).addClass("hide-non-selected");
all_friends.not( /^[a-jA-J]+$/.test(all_friends.find(".friend-name").html().charAt(0)) ).addClass("hide-non-selected");
Final Solution I used using jquery filter (Barmar's answer):
all_friends.filter(function() {
return(!(/^[a-fA-F]+$/.test($(this).find(".friend-name").html().charAt(0))))
}).addClass("hide-non-selected");