Savage Savage -4 years ago 164
jQuery Question

jQuery selector to exclude parents with a specific class

I have the following query to select all

input
and
select
elements of a certain type, and I need to extend it to exclude all items whose parents have a certain class:

$('input,select')
.not('[type="checkbox"],[type="radio"],[type="button"],[type="submit"]')
.not(':parent.no-field-hint') // <-- NEED TO FIX THIS
.on('focus', function () {
// do something
});


How can I modify the line in question?

Answer Source

The logic here is a little too complicated for a selector, aside from the fact there's no :parent selector. You could use filter() to achieve what you need though:

$('input, select').not('[type="checkbox"], [type="radio"], [type="button"], [type="submit"]').filter(function() {
  return !$(this).parent().hasClass('no-field-hint');
}).on('focus', function () {
    // do something
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download