Reputation: 23
I'm hoping to get some assistance with a little jQuery script. I've been going through all the posts I can find here on stackoverflow and been trying to piece things together to achieve this result.
The result I'm after is this - If a class .fes-el also has the class .fn_tags_field
Add the class .updated_parent to the parent class.
I'm not really experienced with programming in any sort of way. So I'm probably messing up something really basic. Any help would be appreciated.
This is what I've been able to piece together so far. The reason I want to use .parent() is because there are multiple elements with the class .fes-el. My previous attempts without usiing .parent() resulted in all parent classes getting .updated_parent added.
jQuery(document).ready(function($) {
if ($(".fes-el").hasClass('fn_tags_field')) {
$(this).parent().addClass('updated_parent');
}
});
This is how the html is made up.
<div class="fes-field fn_container_start">
<div class="fes-el fn_tags_field download_tag">
<div class="fes-label"></div>
<div class="fes-fields"></div>
</div>
</div>
My hopes were that using that bit of jQuery would add the class .updated_parent after fn_container_start
I appreciate all the help I can get.
Regards, Morten
Upvotes: 0
Views: 1342
Reputation: 78
function JSFunc(){
$(".fes-el.fn_tags_field").each(function (index, value) {
$(value).parent().addClass("updated_parent");
})
}
This will loop through all of the divs find the divs that match all the required classes and add your updated class to the parent.
This is not an optimized solution since you havent said when this would need to happen, all that you have to do is execute this code whenever you need to update the classes
Upvotes: 2