Given the following XML:
<users>
<user state="CA" sex="m">Max</user>
<user state="AZ" sex="f">Jen</user>
<user state="OR" sex="f">Kim</user>
<user state="NV" sex="m">Bob</user>
<user state="CA" sex="m">Jon</user>
<user state="AZ" sex="m">Jim</user>
<user state="OR" sex="f">Joy</user>
<user state="NV" sex="f">Amy</user>
</users>
Using jQuery, is there a way to select users who are male and are either from CA or NV, but without using the filter function? To be clear, I know that
$(xml).find("user[sex='m']")
selects only male users, while
$(xml).find("user[state='CA'],[state='NV']")
selects all users from either CA or NV. But I am not able to combine both of them with a logical AND within a single selector.
Using the filter function, however, the following works:
$(xml).find("user").filter(function() {
return $(this).attr('sex') == 'm' && ($(this).attr('state') == 'CA' || $(this).attr('state') == 'NV')
}).each(function() {
alert($(this).text());
});
Thanks!