Bolteh
Bolteh

Reputation: 57

Add new class to parent if child has a specific class

With my limited knowledge (or lack thereof) of JS, I'm trying to figure out a way to add a class to the parent of a specific element.

If been using this Fiddle to try to get it to work, but to no avail: https://jsfiddle.net/s0pca3cs/

if ($('.form__ select select').hasClass('error')) {
$(this).parent().addClass('active');
};

Anyone want to pinpoint here I mess it up? Thanks

Upvotes: 0

Views: 35

Answers (1)

BenM
BenM

Reputation: 53246

You cannot use the this keyword in this context.

$('.form__select select.error').parent().addClass('active');

Demo

Upvotes: 2

Related Questions