Lydia Kirubai S
Lydia Kirubai S

Reputation: 9

Add a new class to the parent using jQuery inside Magento

I need to add a class named "active" to my parent class "item" using jQuery.

It seems simple but my following code is not adding the new class to the parent.

Note: I am doing this inside form.phtml file inside Magento2.

The code here prints the alert but not adding the class.

   jQuery('.item-info').click(function(e){
      $(this).parent().addClass('active');
        // alert('test');
  });

This is my html:

<tbody class="cart item">
<tr class="item-info">

Expected html:

<tbody class="cart item active">
<tr class="item-info">

Update - As per the comment

require(['jquery'], function($) {
  $('.item-info').each(function() {
      $(this).closest("tbody").addClass('free-item');
  });
});

But this adds to every tbody. I am doing this on a cart page where the products are displayed.

Upvotes: -1

Views: 48

Answers (0)

Related Questions